如何渲染具有自訂紙張尺寸的PDF

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

查克尼思·賓

自訂紙張大小指的是用戶定義的非標準紙張大小,而不是像A4或信紙尺寸這樣的標準大小。 (8.5 x 11 英吋)自訂紙張尺寸通常用於打印需要獨特或特殊佈局的文件,例如海報、橫幅或專業文件。

探索IronPDF提供的廣泛紙張尺寸範圍,提供多種選擇以滿足您的需求。!


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
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")
VB   C#

輸出 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")
VB   C#

輸出 PDF

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。