如何在 C# 中使用自定义纸张大小渲染 PDF
IronPDF允许您使用ChromePdfRenderer类在 C# 中渲染具有自定义纸张尺寸的 PDF,并通过 SetCustomPaperSizeInInches() 等方法设置特定尺寸,从而可以精确控制文档尺寸,以用于海报或横幅等特殊布局。
自定义纸张尺寸指的是由用户定义的非标准纸张尺寸,而不是像A4或信纸(8.5 x 11英寸)这样的标准尺寸。 自定义纸张尺寸通常用于打印需要独特或特定布局的文档,例如海报、横幅或专业文件。 在处理需要特定尺寸的 HTML 到 PDF 的转换项目时,这种灵活性至关重要。
通过IronPDF发现广泛的纸张尺寸选择,提供各种选项以满足您的需求!
快速入门:在IronPDF中定义自定义纸张尺寸
在本快速指南中,学习如何仅使用几行代码设置IronPDF的自定义纸张尺寸。 使用IronPDF,您可以通过定义任意单位的精确宽度和高度轻松定制PDF的尺寸。 这种灵活性非常适合创建具有独特布局要求的文档,如海报或横幅。 首先通过 NuGet 下载 IronPDF 库,然后按照此示例毫不费力地设置所需的纸张大小。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf
PM > Install-Package IronPdf -
复制并运行这段代码。
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") -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPDF
最小工作流程(5 个步骤)
- 从 NuGet 下载 IronPDF,用于在 PDF 中设置自定义纸张大小。
- 在C#中实例化ChromePdfRenderer类
- 在新对象上访问RenderingOptions
- 根据测量单位调用 `SetCustomPaperSize` 方法之一
- 渲染并导出PDF文档
如何使用标准纸张尺寸?
首先,创建 ChromePdfRenderer 类的一个实例。 然后,使用新创建对象的 RenderingOptions 属性来修改 PaperSize。 请将其设置为 PdfPaperSize 枚举中的预定义值之一,以指定所需的纸张尺寸。我们提供 100 多种预定义的标准纸张尺寸,方便您使用。
在使用 PDF 渲染选项时,IronPDF 可全面控制文档的格式化方式。 标准纸张尺寸包括 A4、Letter、Legal 等常用格式以及许多国际标准。
有哪些标准纸张尺寸?
以下是设置标准纸张尺寸的示例:
: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")
IronPDF 支持大量标准纸张尺寸,包括
- ISO 216 系列:A0 至 A10、B0 至 B10
- 北美文:信函、法律文件、小报、行政文件
- 架构:ANSI A 到 E
- 日语:JIS B0 至 B10
- 信封尺寸:各种国际信封标准
有关可用纸张尺寸及其尺寸的完整列表,请参阅 API 参考文档。
哪些属性控制纸张尺寸?
PaperSize: 为 PDF 页面设置输出纸张尺寸,预定义尺寸包括 letter、A3、A4 等。ForcePaperSize: 通过在从 HTML 生成 PDF 后调整页面大小,强制页面大小与通过IronPdf.ChromePdfRenderOptions.PaperSize指定的大小完全一致。 此功能有助于绕过指定纸张尺寸的CSS规则。
将这些属性与 自定义页边距结合使用时,您可以实现对 PDF 排版的精确控制。
如何获得不同单位的标准纸张尺寸?
需要找到标准纸张尺寸的尺寸吗? 您可以使用 ToMillimeters 方法轻松完成此操作。 此方法返回一个元组,其中包含标准纸张尺寸的宽度和高度,作为 Length 对象。 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()
在与CSS响应式布局集成时,或需要计算自定义布局的精确尺寸时,该功能尤其有用。
如何创建自定义纸张尺寸?
首先,我们实例化 ChromePdfRenderer 类。 从新创建的对象中,我们可以访问 RenderingOptions,将自定义纸张尺寸应用于新生成的 PDF 文档。 有四种方法可用于设置PDF页面的输出纸张尺寸,每种方法基于不同的测量单位:
SetCustomPaperSizeInCentimeters: 尺寸在centimeters。SetCustomPaperSizeInInches: 尺寸在inches。SetCustomPaperSizeInMillimeters: 尺寸在millimeters。SetCustomPaperSizeInPixelsOrPoints: 尺寸在pixels or points。
在创建自定义纸张尺寸时,必须考虑它们将如何与页眉和页脚以及页面方向设置进行交互。
自定义尺寸可以使用哪些单位?
以下是如何以厘米为单位设置自定义纸张尺寸的示例:
: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")
以下是每个计量单位的其他示例:
// Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5);
// Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420); // A3 size
// Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96); // 96 DPI
// Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5);
// Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420); // A3 size
// Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96); // 96 DPI
' Example: Custom paper size in inches (for US letter-like custom size)
renderer.RenderingOptions.SetCustomPaperSizeInInches(8.5, 11.5)
' Example: Custom paper size in millimeters (for precise metric measurements)
renderer.RenderingOptions.SetCustomPaperSizeInMillimeters(297, 420) ' A3 size
' Example: Custom paper size in pixels (useful for screen-based layouts)
renderer.RenderingOptions.SetCustomPaperSizeInPixelsOrPoints(1024, 768, 96) ' 96 DPI
在使用自定义尺寸时,您可能还需要探索视口和缩放设置,以确保您的内容在自定义尺寸内适合。
如何修改纸张尺寸?
在现有的 PDF 文档或新渲染的 PDF 中,可以使用 ExtendPage 方法修改每一页的大小。 此方法允许您指定目标页面索引、各四边的修改值和测量单位。 每侧的值可以是负值,即减少该特定侧,也可以是正值,即延长该侧。
当您需要在创建 PDF 后对其进行调整时,如合并不同页面尺寸的多个 PDF 或准备打印文档时,该功能尤其有用。
ExtendPage 接受哪些参数?
ExtendPage 方法接受以下参数:
- 页面索引:要修改的页面的零基索引
- 左侧扩展:扩展/缩减左侧的金额
- 右侧扩展:扩展/缩减右侧的金额
- 顶部扩展:扩展/缩减顶面的金额
- 底部扩展:扩展/缩减底面的数量
-计量单位:计量单位(
millimeters, inches, etc.)
以下是如何修改纸张尺寸的示例:
: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")
下面是一个更全面的示例,展示了各种页面修改:
// Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter);
// Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter);
// Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter);
// Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch);
// Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter);
// Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter);
// Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter);
// Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch);
' Extend all sides equally
pdf.ExtendPage(0, 10, 10, 10, 10, MeasurementUnit.Millimeter)
' Reduce page size (negative values)
pdf.ExtendPage(1, -20, -20, -10, -10, MeasurementUnit.Millimeter)
' Extend only top and bottom (useful for adding header/footer space)
pdf.ExtendPage(2, 0, 0, 25, 25, MeasurementUnit.Millimeter)
' Work with inches instead of millimeters
pdf.ExtendPage(3, 0.5, 0.5, 1, 1, MeasurementUnit.Inch)
自定义纸张尺寸的最佳实践
在 IronPDF 中使用自定义纸张尺寸时,请考虑以下最佳实践:
1.测试不同的单位:在使用自定义尺寸时,请测试哪种测量单位最适合您的使用案例。 像素非常适合屏幕布局,而毫米或英寸则更适合打印。
2.考虑打印页边距:在创建用于打印的 PDF 时,请记住要考虑打印机页边距。 大多数打印机无法打印到纸张边缘。
3.响应式设计:在将 HTML 转换为 PDF 时,请确保您的 HTML 采用响应式设计原则,以适应不同的纸张尺寸。
4.性能优化:自定义纸张尺寸过大会影响性能。 考虑对大型文档使用 压缩。
5.兼容性:在不同的 PDF 阅读器中测试自定义大小的 PDF,以确保兼容性,尤其是在使用非标准尺寸的情况下。
准备好看看您还能做些什么吗? 查看我们的教程页面:创建PDF
常见问题解答
如何在 C# 中为 PDF 文档设置自定义纸张大小?
使用 IronPDF,您可以使用 ChromePdfRenderer 类设置自定义纸张尺寸。只需将 RenderingOptions 中的 PaperSize 属性设置为 PdfPaperSize.Custom,然后使用 SetCustomPaperSizeInInches() 等方法定义特定尺寸即可。例如:renderer.RenderingOptions.SetCustomPaperSizeInInches(5, 7)。
自定义纸张尺寸可以使用哪些测量单位?
IronPDF 通过不同的 SetCustomPaperSize 方法支持自定义纸张尺寸的多种测量单位。您可以以英寸、厘米、毫米或像素为单位指定尺寸,从而灵活地满足各种国际标准和项目要求。
何时需要使用自定义纸张尺寸而不是标准尺寸?
IronPDF 中的自定义纸张尺寸是创建具有独特布局(如海报、横幅、专业文档或任何不适合 A4 或 Letter 等标准格式的设计)的 PDF 的理想选择。这种灵活性对于需要特定尺寸的 HTML 到 PDF 转换项目尤其有用。
有多少种预定义的标准纸张尺寸?
IronPDF 通过 PdfPaperSize 枚举提供了 100 多种预定义的标准纸张尺寸,包括 A4、Letter、Legal 等常用格式以及许多国际标准,为大多数文档需求提供了全面的选择。
实现自定义纸张尺寸的步骤是什么?
使用 IronPDF 实现自定义纸张尺寸:1) 通过 NuGet 下载 IronPDF,2) 创建一个 ChromePdfRenderer 实例,3) 访问 RenderingOptions 属性,4) 使用所需的尺寸调用 SetCustomPaperSize 方法,5) 渲染并保存 PDF 文档。

