如何渲染帶自訂紙張尺寸的PDF | IronPDF

如何在C#中使用自定義紙張大小渲染PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF使您能夠在C#中使用SetCustomPaperSizeInInches()等方法設置特定尺寸來渲染具有自定義紙張大小的PDF,從而精確控制文件尺寸,適用於海報或橫幅等特殊佈局。

自定義紙張大小是指由使用者定義的非標準紙張大小,而不是像A4或信封大小(8.5 x 11英寸)的標準大小。 自定義紙張大小通常用於列印需要獨特或特定佈局的文件,例如海報、橫幅或特殊文件。 這種靈活性在從事需要特定尺寸的HTML到PDF轉換項目時至關重要。

了解IronPDF提供的廣泛紙張大小範圍,提供多種選擇以滿足您的需求!

快速入門:在IronPDF中定義自定義紙張大小

在這本快速指南中,學習如何使用IronPDF僅用幾行程式碼設置自定義紙張大小。 使用IronPDF,您可以輕鬆地通過定義任何您喜歡的單位的精確寬度和高度來量身定制PDF尺寸。 這種靈活性非常適合建立具有獨特佈局要求的文件,比如海報或橫幅。 首先通過NuGet下載IronPDF程式庫,並遵循此範例輕鬆設置您所需的紙張大小。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼片段。

    var renderer = new IronPdf.ChromePdfRenderer { RenderingOptions = { PaperSize = IronPdf.Rendering.PdfPaperSize.Custom } };
    renderer.RenderingOptions.SetCustomPaperSizeInInches(5, 7);
    renderer.RenderHtmlAsPdf("<h1>Custom size</h1>").SaveAs("custom-size.pdf")
  3. 部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronPDF,透過免費試用

    arrow pointer


我如何使用標準紙張大小?

首先,建立ChromePdfRenderer類的實例。 然後,使用新建立的物件的PaperSize的修改。 將其設置為PdfPaperSize枚舉中的預定義值之一,以指定所需的紙張大小。我們提供超過100種預定義的標準紙張大小以方便您使用。

在使用PDF渲染選項時,IronPDF提供對文件格式的全面控制。 標準紙張大小包括常用格式,例如A4、信封、法律和許多國際標準。

有哪些標準紙張大小可用?

以下是如何設置標準紙張大小的範例:

:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-standard-paper-size.cs
using IronPdf;
using IronPdf.Rendering;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set paper size to A4
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Standard Paper Size</h1>");

pdf.SaveAs("standardPaperSize.pdf");
Imports IronPdf
Imports IronPdf.Rendering

Private renderer As New ChromePdfRenderer()

' Set paper size to A4
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Standard Paper Size</h1>")

pdf.SaveAs("standardPaperSize.pdf")
$vbLabelText   $csharpLabel

IronPDF支持大量的標準紙張大小集合, 包括:

  • ISO 216系列:從A0到A10,從B0到B10
  • 北美:信紙、法律、報紙、袖珍
  • 建築:從ANSI A到E
  • 日本:從JIS B0到B10
  • 信封尺寸:各種國際信封標準

完整的可用紙張大小及其尺寸列表,請參閱API參考文件

哪些屬性控制紙張大小?

  • PaperSize:為PDF頁面設置輸出紙張大小,具有信紙、A3、A4等預定義大小。
  • IronPdf.ChromePdfRenderOptions.PaperSize調整PDF生成後的頁面,使得頁面大小與規定的一致。 此功能有助於繞過指定紙張大小的CSS規則。

在與自定義邊距結合使用這些屬性時,可以實現對PDF佈局的精確控制。

我如何在不同單位中獲得標準紙張大小?

需要查找標準紙張大小的尺寸嗎? 您可以輕鬆使用ToMillimeters方法。 此方法返回一個元組,其中包含標準紙張大小的寬度和高度,為Length物件。 Length類非常靈活,允許您輕鬆地將這些尺寸轉換為多種單位,包括:

  • 毫米
  • 釐米
  • 英寸
  • 像素
:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-standard-paper-size-in-other-unit.cs
using IronPdf.Rendering;

double A4WidthInPixel = PdfPaperSize.A4.ToMillimeters().width.ToPixel();
double A4HeightInCentimeter = PdfPaperSize.A4.ToMillimeters().height.ToCentimeter();
Imports IronPdf.Rendering

Private A4WidthInPixel As Double = PdfPaperSize.A4.ToMillimeters().width.ToPixel()
Private A4HeightInCentimeter As Double = PdfPaperSize.A4.ToMillimeters().height.ToCentimeter()
$vbLabelText   $csharpLabel

此功能特別有用於結合CSS響應式佈局或需要為自定義佈局計算精確尺寸時。


如何建立自定義紙張大小?

首先,我們開始實例化ChromePdfRenderer類。 從新建立的物件中,我們可以存取RenderingOptions來將自定義紙張大小應用於新生成的PDF文件。 有四種方法可用來設置PDF頁面的輸出紙張大小,每種方法基於不同的測量單位:

  • centimeters
  • inches
  • millimeters
  • pixels or points

在建立自定義紙張大小時,重要的是要考慮它們如何與頁眉和頁腳以及頁面方向設置交互。

我可以使用哪些單位來設置自定義尺寸?

以下是如何將紙張大小設置為釐米的範例:

:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-cm.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set custom paper size in cm
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(15, 15);

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Custom Paper Size</h1>");

pdf.SaveAs("customPaperSize.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

' Set custom paper size in cm
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(15, 15)

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Custom Paper Size</h1>")

pdf.SaveAs("customPaperSize.pdf")
$vbLabelText   $csharpLabel

以下是為每種測量單位準備的其他範例:

// Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5);

// Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420); // A3 size

// Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96); // 96 DPI
// Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5);

// Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420); // A3 size

// Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96); // 96 DPI
' Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5)

' Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420) ' A3 size

' Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96) ' 96 DPI
$vbLabelText   $csharpLabel

在使用自定義尺寸時,您可能還想探索視區和縮放設置以確保您的內容正確地適合於自定義尺寸中。


Icon Quote related to 我可以使用哪些單位來設置自定義尺寸?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 我可以使用哪些單位來設置自定義尺寸?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 我可以使用哪些單位來設置自定義尺寸?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 我可以使用哪些單位來設置自定義尺寸?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 我可以使用哪些單位來設置自定義尺寸?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 我可以使用哪些單位來設置自定義尺寸?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

我如何修改紙張尺寸?

在現有的PDF文件或新渲染的PDF中,每頁的大小可以通過ExtendPage方法進行修改。 此方法允許您指定目標頁面的索引、修改四個邊的值以及測量單位。 每一側的值可以為負,這將減少該特定側,或為正,這將延伸該側。

此功能特別有用於需要在建立PDF後進行調整時,例如合併多個具有不同頁面大小的PDF或在準備列印文件時。

ExtendPage接受哪些參數?

ExtendPage方法接受以下參數:

  • 頁面索引:要修改的頁面的零基索引
  • 左側延伸:左側延伸/減少的量
  • 右側延伸:右側延伸/減少的量
  • 上側延伸:上側延伸/減少的量
  • 底部延伸:底部延伸/減少的量
  • 測量單位:測量單位(millimeters, inches, etc.)

以下是如何修改紙張尺寸的範例:

:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-modify-paper-size.cs
using IronPdf;
using IronPdf.Editing;

PdfDocument pdf = PdfDocument.FromFile("customPaperSize.pdf");

pdf.ExtendPage(0, 50, 0, 0, 0, MeasurementUnit.Millimeter);

pdf.SaveAs( "extendedLeftSide.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private pdf As PdfDocument = PdfDocument.FromFile("customPaperSize.pdf")

pdf.ExtendPage(0, 50, 0, 0, 0, MeasurementUnit.Millimeter)

pdf.SaveAs("extendedLeftSide.pdf")
$vbLabelText   $csharpLabel

這裡有一個更全面的例子,顯示了各種頁面修改:

// Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter);

// Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter);

// Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter);

// Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch);
// Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter);

// Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter);

// Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter);

// Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch);
' Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter)

' Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter)

' Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter)

' Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch)
$vbLabelText   $csharpLabel

自定義紙張大小的最佳實踐

在IronPDF中使用自定義紙張大小時,請考慮以下最佳實踐:

  1. 測試不同的單位:當使用自定義尺寸時,測試哪一種測量單位最適合您的使用情境。 像素非常適合基於螢幕的布局,而毫米或英寸更適合於列印。

  2. 考慮列印邊距:建立列印用PDF時,請記得考慮列印機邊距。 大多數列印機不能列印到紙張的邊緣。

  3. 響應式設計:將HTML轉換為PDF時,確保您的HTML使用響應式設計原則以適應不同的紙張大小。

  4. 性能優化:非常大的自定義紙張大小可能會影響性能。 考慮對大型文件使用壓縮

  5. 相容性:在不同的PDF查看器中測試您的自定義大小PDF以確保相容性,特別是如果使用非標準尺寸。

準備好看看您還能做什麼嗎? 查看我們的教學頁面:建立PDF

常見問題

如何在C#中為PDF文件設置自訂紙張尺寸?

使用IronPDF,您可以使用ChromePdfRenderer類設置自訂紙張尺寸。只需將RenderingOptions中的PaperSize屬性設置為PdfPaperSize.Custom,然後使用SetCustomPaperSizeInInches()等方法來定義您的特定尺寸。例如:renderer.RenderingOptions.SetCustomPaperSizeInInches(5, 7)。

我可以用哪些測量單位來設置自訂紙張尺寸?

IronPDF 支援通過不同的 SetCustomPaperSize 方法來設置自訂紙張尺寸的多種測量單位。您可以指定以英寸、厘米、毫米或像素為單位的尺寸,使其能靈活應對各種國際標準和專案需求。

什麼時候我需要使用自訂紙張尺寸而不是標準尺寸?

IronPDF 中的自訂紙張尺寸非常適合用於建立具有獨特佈局的PDF,例如海報、橫幅、特殊文件或任何不適合標準格式如A4或信函尺寸的設計。這種靈活性在需要特定尺寸的HTML到PDF轉換項目中特別有用。

有多少預定義的標準紙張尺寸可用?

IronPDF 透過 PdfPaperSize 列舉提供了超過100種預定義的標準紙張尺寸,包括常用的格式如 A4、信函尺寸、法律尺寸,及許多國際標準,為大多數文件需求提供了全面的選擇。

實施自訂紙張尺寸的步驟是什麼?

要在IronPDF中實現自訂紙張尺寸:1)通過NuGet下載IronPDF,2)建立ChromePdfRenderer實例,3)存取RenderingOptions屬性,4)使用您想要的尺寸調用SetCustomPaperSize方法,5)渲染並保存您的PDF文件。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 19,936,792 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想快速獲得證明嗎? PM > Install-Package IronPdf
執行範例 看您的HTML變成PDF。