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

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

查克尼思·賓

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

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


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

第一步:
green arrow pointer

使用自訂紙張大小範例

首先,我們開始實例化 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頁面設定一個預定義尺寸的輸出紙張大小,例如信紙、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")
VB   C#

輸出 PDF

Chaknith related to 輸出 PDF

查克尼思·賓

軟體工程師

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