如何在 C# 中设置打印纸张边距
打印边距控制着文档内容与物理页面边缘之间的空白区域。 正确处理这些内容可避免文本被截断,确保不同打印机上的版式一致,并满足发票、报告及法律文件的排版要求。
IronPrint 的 Margins 类支持毫米单位的数值,并提供三种构造函数重载——统一、水平/垂直以及单边——因此我们只需一行代码即可满足任何版式要求。下面我们将逐一介绍这些方法,从安装到使用自定义边距进行打印。
快速入门:设置纸张边距
- 通过 NuGet 安装 IronPrint:
Install-Package IronPrint - 在文件中添加
using IronPrint; - 创建一个
PrintSettings对象 - 将
Margins的值赋给PaperMargins(单位为毫米) - 将设置(包含文件路径)传递给
Printer.Print()
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
复制并运行这段代码。
using IronPrint; // Set 15 mm margins on all sides and print PrintSettings settings = new PrintSettings(); settings.PaperMargins = new Margins(15); Printer.Print("report.pdf", settings); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 为
PaperMargins赋值Margins - 将设置传递给
Printer.Print() - 运行该项目以自定义页边距进行打印
如何设置四边等宽页边距?
最简单的构造函数接受一个整数,并将其均匀应用于所有四边。 我们将值以毫米为单位传递:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-uniform-margins.cs
using IronPrint;
// Configure a uniform 20 mm margin on all sides
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
// Print the invoice
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' Configure a uniform 20 mm margin on all sides
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
' Print the invoice
Printer.Print("invoice.pdf", settings)
Margins(20) 将 Right 和 Bottom 分别设置为 20 毫米。 这是标准商业文档中最常见的选择,此类文档仅需在每行两端保持一致的空格即可。
IronPrint 以毫米为单位测量边距,从而避免了 System.Drawing.Printing.Margins 类使用英寸百分之一单位所造成的混淆。 在 IronPrint 中,25.4 毫米的页边距等同于 new System.Drawing.Printing.Margins(100) —— 我们无需进行任何换算。
如何为每一侧设置不同的页边距?
当文档顶部需要额外空间放置页眉,或底部需要额外空间放置页脚时,我们使用四参数构造函数:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-per-side-margins.cs
using IronPrint;
// Configure per-side margins (left, top, right, bottom)
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
// Print the letterhead
Printer.Print("letterhead.pdf", settings);
Imports IronPrint
' Configure per-side margins (left, top, right, bottom)
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
' Print the letterhead
Printer.Print("letterhead.pdf", settings)
参数顺序为 left, top, right, bottom。 每个数值都是独立的,因此我们可以创建非对称布局,以适应页眉、页脚、装订边缘或打孔区域。 Margins 类的 API 参考文档对每个字段进行了详细说明。
常见的页边距布局有哪些简写选项?
IronPrint 的 Margins 类除了提供统一版本和单面版本之外,还提供了两个额外的构造函数:
水平/垂直缩写 — Margins(int horizontal, int vertical) 将 left+right 设置为第一个值,top+bottom 设置为第二个值:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-shorthand-margins.cs
using IronPrint;
// Configure horizontal and vertical margin shorthand
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
// Print the landscape report
Printer.Print("report-landscape.pdf", settings);
Imports IronPrint
' Configure horizontal and vertical margin shorthand
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
' Print the landscape report
Printer.Print("report-landscape.pdf", settings)
零边距 — Margins.Zero 移除所有边距以实现无边框打印:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-zero-margins.cs
using IronPrint;
// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
PaperMargins = new Margins(0)
};
// Print the poster
Printer.Print("poster.png", borderless);
Imports IronPrint
' Configure zero margins for edge-to-edge printing
Dim borderless As New PrintSettings With {
.PaperMargins = New Margins(0)
}
' Print the poster
Printer.Print("poster.png", borderless)
请注意,大多数实体打印机都有硬件规定的最小可打印区域。 设置 Margins.Zero 会向驱动程序发送零边距指令,但根据打印机的具体性能,边缘附近的内容仍可能被裁切。
如何将页边距与其他打印设置结合使用?
PaperMargins 是 PrintSettings 上的一个属性。 我们可以将纸张尺寸、方向、DPI、份数、灰度模式和打印机选择整合到一个配置对象中:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-combined-settings.cs
using IronPrint;
// Configure full print settings with asymmetric margins
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(15, 20, 15, 25),
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PrinterName = "HP LaserJet Pro MFP M428"
};
// Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings);
Imports IronPrint
' Configure full print settings with asymmetric margins
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(15, 20, 15, 25),
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PrinterName = "HP LaserJet Pro MFP M428"
}
' Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings)
对于异步工作流(WPF、MAUI 或 ASP.NET Web 应用程序),请将 Printer.Print() 替换为 await Printer.PrintAsync(),以避免阻塞 UI 线程。 同一个 PrintSettings 对象可同时适用于这两种方法。
我的下一个步骤是什么?
我们介绍了使用 IronPrint 配置打印边距的四种方法:使用 Margins(int) 设置统一边距、使用 Margins(int, int, int, int) 进行每页单独控制、使用 Margins(int, int) 的水平/垂直简写,以及使用 Margins.Zero 进行无边距打印。 每种方法都集成到 PrintSettings.PaperMargins 中,并兼容 Printer.Print() 和 Printer.PrintAsync()。
欲了解更多信息,请探索以下资源:
- IronPrint 教程 — 文档打印,提供端到端打印操作指南。
- 打印设置指南:涵盖 DPI、纸张方向、份数等设置。
Margins类 API 参考文档,包含完整的构造函数和字段说明。Printer所有静态打印方法的类 API 参考。
常见问题解答
什么是IronPrint,它如何帮助设置C#中的打印边距?
IronPrint是一个.NET库,它简化了设置C#中的打印边距。它提供了一个Margins类,允许开发人员通过一行代码轻松地自定义统一的、单独一边的和无边框的打印边距。
如何使用IronPrint在C#中设置统一的打印边距?
要在C#中使用IronPrint设置统一的打印边距,您可以使用Margins类。该类能够在一行代码中为页面的所有边设置相同的边距大小。
是否可以为页面的每一边设置不同的边距?
是的,IronPrint允许您在C#中为页面的每一边设置不同的边距。Margins类提供了自定义上、下、左、右边距的选项。
我可以使用IronPrint创建无边框打印吗?
IronPrint支持创建无边框打印。通过使用Margins类调整边距,您可以将边距设置为零,从而有效地产生无边框打印。
使用IronPrint设置纸张边距有什么好处?
IronPrint通过提供一个直接且有效的API简化了在C#中设置纸张边距的过程。它的Margins类简化了代码并提高了生产力,使实现自定义打印需求变得更容易。
设置IronPrint的打印边距是否需要丰富的编码知识?
不,使用IronPrint设置打印边距不需要丰富的编码知识。该库设计为用户友好,即使是具有基本C#技能的人也可以轻松地实现自定义边距。
IronPrint在设置边距时如何处理不同的纸张尺寸?
IronPrint允许您指定适合文档特定尺寸的边距,以适应各种纸张尺寸。这种灵活性确保您的打印输出满足您的特定布局需求。
IronPrint可以与其他.NET应用程序集成吗?
是的,IronPrint可以与其他.NET应用程序无缝集成,允许您在广泛的项目和工作流中合并自定义打印边距设置。

