如何用 C# 設置 PDF 的自訂紙張大小 | IronPDF

如何在 C# 中渲染具有自訂紙張尺寸的 PDF

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

IronPDF可讓您使用ChromePdfRenderer類別在 C# 中渲染具有自訂紙張尺寸的 PDF,並透過 SetCustomPaperSizeInInches() 等方法設定特定尺寸,從而可以精確控製文件尺寸,以用於海報或橫幅等特殊佈局。

自訂紙張尺寸是指使用者定義的非標準紙張尺寸,而不是像 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 類別的一個實例。 然後,使用新建立物件的 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");
$vbLabelText   $csharpLabel

IronPDF支援多種標準紙張尺寸,包括:

  • ISO 216 系列:A0 至 A10,B0 至 B10 -北美:信函、法律、小報、行政 -建築:符合 ANSI A 至 E 標準 -日文:JIS B0 至 B10 信封尺寸:各種國際信封標準

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

哪些因素決定紙張尺寸?

  • PaperSize: 為 PDF 頁面設定輸出紙張尺寸,預先定義尺寸包含 letter、A3、A4 等。
  • ForcePaperSize: 透過從 HTML 產生 PDF 後調整頁面大小,強制頁面大小與透過 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();
$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");
$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
$vbLabelText   $csharpLabel

使用自訂尺寸時,您可能還需要檢查視窗和縮放設置,以確保您的內容能夠正確地適應自訂尺寸。


如何修改紙張尺寸?

在現有的 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");
$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);
$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 或 Letter 等標準格式的設計。這種靈活性對於需要特定尺寸的 HTML 至 PDF 轉換專案尤其有用。

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

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

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

要使用 IronPDF 實現自定義紙張大小:1) 透過 NuGet 下載 IronPDF,2) 建立一個 ChromePdfRenderer 實例,3) 存取 RenderingOptions 屬性,4) 以您所需的尺寸呼叫 SetCustomPaperSize 方法,5) 渲染並儲存您的 PDF 文件。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。