如何渲染具有自訂紙張尺寸的PDF
自訂紙張大小指的是用戶定義的非標準紙張大小,而不是像A4或信紙尺寸這樣的標準大小。 (8.5 x 11 英吋)自訂紙張尺寸通常用於打印需要獨特或特殊佈局的文件,例如海報、橫幅或專業文件。
探索IronPDF提供的廣泛紙張尺寸範圍,提供多種選擇以滿足您的需求。!
如何渲染具有自訂紙張尺寸的PDF
- 下載 C# 庫來設置自定義紙張大小
- 實例化 ChromePdfRenderer C# 類別
- 訪問新對象的RenderingOptions
- 調用其中之一
設定自訂紙張大小
基於測量單位的方法 - 渲染和匯出PDF文件
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronPDF 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。
Install-Package IronPdf
請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip
手動安裝到您的項目中
下載DLL使用自訂紙張大小範例
首先,我們開始實例化 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 頁面的輸出紙張大小,可選擇預定義的尺寸,例如 letter、A3、A4 等等。
- ForcePaperSize: 強制頁面尺寸精確符合通過 IronPdf.ChromePdfRenderOptions.PaperSize 指定的尺寸,在從 HTML 生成 PDF 後重新調整頁面尺寸。此功能對於繞過指定紙張大小的 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")