如何渲染具有自訂紙張尺寸的PDF
自訂紙張大小指的是用戶定義的非標準紙張大小,而不是像A4或信紙尺寸這樣的標準大小。(8.5 x 11 英吋). 自定義紙張大小常用於打印需要獨特或特定佈局的文件,如海報、橫幅或特殊文件。
探索 IronPDF 提供的廣泛紙張尺寸範圍,提供各種選擇以滿足您的需求。!
如何渲染具有自訂紙張尺寸的PDF
- 從 NuGet 下載 IronPDF 以在 PDF 中設置自定義紙張大小
- 實例化 ChromePdfRenderer C# 類別
- 訪問新對象的RenderingOptions
- 調用其中之一
設定自訂紙張大小
基於測量單位的方法 - 渲染和匯出PDF文件
立即在您的專案中使用IronPDF,並享受免費試用。
使用自訂紙張大小範例
首先,我們開始實例化 ChromePdfRenderer 類。 從新創建的對象中,我們可以訪問 RenderingOptions 以將自定義紙張大小應用於新生成的 PDF 文件。 有四種方法可以用來設定PDF頁面的輸出紙張大小,每種方法基於不同的測量單位:
SetCustomPaperSizeInCentimeters
:尺寸以公分為單位。SetCustomPaperSizeInInches
:尺寸以英寸為單位。SetCustomPaperSizeInMillimeters
:尺寸以毫米為單位。SetCustomPaperSizeInPixelsOrPoints
:尺寸以像素或點表示。
代碼
: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")
輸出 PDF
相關屬性
- PaperSize:為PDF頁面設定一個預定義尺寸的輸出紙張大小,例如信紙、A3、A4等。
- 強制紙張尺寸:通過在從HTML生成PDF後調整頁面大小,強制頁面尺寸完全符合IronPdf.ChromePdfRenderOptions.PaperSize的指定。 此功能可用於繞過指定紙張大小的CSS規則。
修改紙張尺寸範例
在現有的PDF文件或新渲染的PDF中,可以使用ExtendPage
方法修改每個頁面的大小。 此方法允許您指定目標頁面索引、修改四個邊的值以及測量單位。 每個邊的值可以是負的,這會減少該邊,或是正的,這會延伸該邊。
代碼
: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")