如何使用 C# 設定 PDF 的自訂紙張尺寸 | IronPDF

How to Render PDFs with Custom Paper Size

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

自定紙張大小是指由使用者自行定義的非標準紙張大小,而不是標準大小如A4或信件大小(8.5 x 11英寸)。 自定紙張大小常用於列印需要特殊或特定佈局的文件,如海報、橫幅或特殊文件。

使用IronPDF探索廣泛的紙張大小範圍,提供豐富的選擇以滿足您的需求!

快速開始:在IronPDF中定義自定紙張大小

在這份快速指南中,學習如何使用IronPDF設置自定紙張大小,只需幾行代碼即可。 有了IronPDF,您可以輕鬆調整PDF尺寸,按您所需單位精確定義寬度和高度。 這種靈活性非常適合創建具有特定佈局需求的文件,如海報或橫幅。 首先,通過NuGet下載IronPDF庫,並按照該示例輕鬆設置您想要的紙張大小。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    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. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5步)

  1. 通過NuGet下載IronPDF來設置PDF中的自定紙張大小
  2. 在C#中實例化ChromePdfRenderer
  3. 訪問新對象上的RenderingOptions
  4. 根據測量單位調用一個SetCustomPaperSize方法
  5. 渲染並匯出PDF文件


使用標準紙張大小示例

首先,創建ChromePdfRenderer類的實例。 然後,使用新創建對象的RenderingOptions屬性來修改PaperSize。 將其設置為PdfPaperSize枚舉中的一個預定值,指定所需的紙張大小。我們提供超過100個預定義的標準紙張大小以供您方便使用。

代码

以下是如何設置標準紙張大小的示例:

: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");
Imports IronPdf
Imports IronPdf.Rendering

Private renderer As New ChromePdfRenderer()

' Set paper size to A4
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Standard Paper Size</h1>")

pdf.SaveAs("standardPaperSize.pdf")
$vbLabelText   $csharpLabel

相關屬性

  • PaperSize:為PDF頁面設置輸出紙張大小,具有預定義的大小如信件、A3、A4等。
  • ForcePaperSize:強制頁面大小完全根據IronPdf.ChromePdfRenderOptions.PaperSize指定,通過在從HTML生成PDF後調整頁面大小。 此功能對於繞過指定紙張大小的CSS規則非常有用。

以不同單位獲取標準紙張大小

需要找到標準紙張尺寸嗎? 您可以輕鬆地使用ToMillimeters方法。 此方法返回一個元組,其中包含標準紙張尺寸的寬度和高度作為長度對象。 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();
Imports IronPdf.Rendering

Private A4WidthInPixel As Double = PdfPaperSize.A4.ToMillimeters().width.ToPixel()
Private A4HeightInCentimeter As Double = PdfPaperSize.A4.ToMillimeters().height.ToCentimeter()
$vbLabelText   $csharpLabel

使用自定紙張大小示例

首先,我們從實例化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")
$vbLabelText   $csharpLabel

輸出PDF


修改紙張尺寸示例

在現有的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")
$vbLabelText   $csharpLabel

輸出PDF

準備看看您還能做哪些其他事情嗎? 查看我們的教程頁面:創建PDF

常見問題解答

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

要使用 IronPDF 渲染具有自訂紙張尺寸的 PDF,請實例化ChromePdfRenderer類,訪問RenderingOptions ,然後根據您喜歡的測量單位(例如厘米或英寸)使用SetCustomPaperSize方法之一。

如何下載用於自訂紙張尺寸設定的庫?

您可以透過 NuGet 下載 IronPDF,以使用其在 PDF 中設定自訂紙張尺寸的功能。該庫提供了定義 PDF 文件特定尺寸所需的工具。

如何在產生 PDF 檔案時套用標準紙張尺寸?

在 IronPDF 中,建立一個ChromePdfRenderer實例,然後使用RenderingOptionsPaperSize屬性設定為PdfPaperSize列舉中的預定義值,例如 A4 或 letter 尺寸。

是否可以使用 IronPDF 修改現有 PDF 的頁面大小?

是的,IronPDF 允許您使用ExtendPage方法修改現有 PDF 的頁面大小。此方法可讓您透過指定頁面索引和計量單位來調整頁面每一邊的尺寸。

IronPDF中設定自訂紙張尺寸有哪些選項?

IronPDF 提供了使用各種測量單位來設定自訂紙張尺寸的方法: SetCustomPaperSizeInCentimetersSetCustomPaperSizeInInchesSetCustomPaperSizeInMillimetersSetCustomPaperSizeInPixelsOrPoints

ForcePaperSize 功能在 PDF 渲染中是如何運作的?

IronPDF 中的ForcePaperSize功能透過在 HTML 渲染 PDF 後調整頁面大小,確保實際頁面尺寸與指定尺寸一致。這有助於覆蓋任何定義紙張尺寸的 CSS 規則。

IronPDF中有哪些預先定義的紙張尺寸?

IronPDF 提供 100 多種預先定義的紙張尺寸,包括 A3、A4 和信紙尺寸等常用尺寸,可透過PdfPaperSize枚舉進行選擇。

如何使用 IronPDF 將標準紙張尺寸轉換為不同的單位?

您可以使用 IronPDF 的ToMillimeters方法將標準紙張尺寸轉換為毫米、公分、英吋、像素或磅等各種單位,從而在 PDF 設計中提供靈活性。

IronPDF 是否完全相容於 .NET 10?我可以在 .NET 10 專案中使用自訂紙張尺寸功能嗎?

是的,IronPDF 完全支援 .NET 10(包括即將推出或最近發布的版本),您可以在 .NET 10 專案中使用自訂紙張尺寸指南中所述的所有功能(例如SetCustomPaperSizeInInchesSetCustomPaperSizeInMillimeters 、 ForcePaperSizeInInches 、 SetCustomPaperSizeInMillimeters 、 ForcePaperSize等),而相容性

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布