如何使用 C# 為 PDF 設定自訂紙張尺寸 | IronPDF

如何在 C# 中使用自訂紙張尺寸渲染 PDF

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

IronPDF 讓您能夠透過 ChromePdfRenderer 類別,並使用 SetCustomPaperSizeInInches() 等方法設定特定尺寸,在 C# 中渲染具有自訂紙張尺寸的 PDF,從而精確控制文件尺寸,以滿足海報或橫幅等特殊版面的需求。

自訂紙張尺寸是指由使用者定義的非標準紙張尺寸,而非 A4 或信紙尺寸(8.5 x 11 英吋)等標準尺寸。 當需要列印具有獨特或特定版面的文件時,例如海報、橫幅或特殊文件,通常會使用自訂紙張尺寸。 在處理需要特定尺寸的 HTML 轉 PDF 專案時,這種靈活性至關重要。

探索 IronPDF 提供的豐富紙張尺寸選項,多元選擇滿足您的各種需求!

快速入門:在 IronPDF 中定義自訂紙張尺寸

在本快速指南中,您將學習如何僅透過幾行程式碼,使用 IronPDF 設定自訂紙張尺寸。 透過 IronPDF,您可以輕鬆自訂 PDF 尺寸,只需以您偏好的任何單位定義確切的寬度和高度即可。 這種靈活性非常適合製作具有特殊版面配置需求的文件,例如海報或橫幅。 請先透過 NuGet 下載 IronPDF 函式庫,並參照此範例輕鬆設定您所需的紙張尺寸。

  1. using 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 類別的實例。 接著,使用新建立物件的 RenderingOptions 屬性來修改 PaperSize。 請將其設定為 PdfPaperSize 枚舉中的預設值之一,以指定所需的紙張尺寸。我們提供超過 100 種預設標準紙張尺寸,以供您方便選用。

在處理 PDF 渲染選項時,IronPDF 提供對文件格式設定的全面控制。 標準紙張尺寸包含 A4、Letter、Legal 等常用規格,以及多項國際標準。

有哪些標準紙張尺寸可供選擇?

以下是設定標準紙張尺寸的範例:

: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
  • 北美規格:Letter、Legal、Tabloid、Executive
  • 架構:ANSI A 至 E
  • 日文:JIS B0 至 B10
  • 信封尺寸:各種國際信封標準

如需可用紙張尺寸及其尺寸的完整清單,請參閱 API 參考文件

哪些屬性會影響紙張尺寸?

  • PaperSize:為 PDF 頁面設定輸出紙張尺寸,可選用預設尺寸,例如 Letter、A3、A4 等。
  • ForcePaperSize:透過在將 HTML 轉為 PDF/A 後調整頁面大小,強制使頁面尺寸精確符合 IronPdf.ChromePdfRenderOptions.PaperSize 所指定的規格。 此功能有助於繞過指定紙張尺寸的 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 頁面的輸出紙張尺寸,每種方法皆基於不同的測量單位:

  • SetCustomPaperSizeInCentimeters:尺寸單位為 centimeters
  • SetCustomPaperSizeInInches:尺寸單位為 inches
  • SetCustomPaperSizeInMillimeters:尺寸單位為 millimeters
  • SetCustomPaperSizeInPixelsOrPoints:尺寸單位為 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

當使用自訂尺寸時,您可能還需要探索視口和Zoom設定,以確保內容能妥善地適應自訂尺寸。


如何修改紙張尺寸?

在現有的 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. 測試不同單位:在處理自訂尺寸時,請測試哪種度量單位最適合您的使用情境。 像素非常適合用於螢幕版面配置,而毫米或英吋則更適合用於PRINT。

  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 方法,支援自訂紙張尺寸的多種度量單位。您可以指定以英吋、公分、毫米或像素為單位的尺寸,使其能靈活適應各種國際標準與專案需求。

何時需要使用自訂紙張尺寸而非標準尺寸?

當需要建立具有特殊版面的 PDF 檔案時,例如海報、橫幅、特殊文件,或任何不符合 A4 或 Letter 等標準格式的設計,IronPDF 的自訂紙張尺寸功能是理想選擇。這種靈活性對於需要特定尺寸的 HTML 轉 PDF 專案特別有用。

目前提供多少種預設標準紙張尺寸?

IronPDF 透過 PdfPaperSize 枚舉提供超過 100 種預定義標準紙張尺寸,包含 A4、Letter、Legal 等常用格式以及眾多國際標準,為大多數文件需求提供全面的選項。

實作自訂紙張尺寸的步驟有哪些?

若要使用 IronPDF 實作自訂紙張尺寸:1) 透過 NuGet 下載 IronPDF,2) 建立 ChromePdfRenderer 實例,3) 存取 RenderingOptions 屬性,4) 呼叫 SetCustomPaperSize 方法並傳入您所需的尺寸,以及 5) 渲染並儲存您的 PDF 文件。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。