如何在 C# 中设置打印纸张边距

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

打印边距控制着文档内容与物理页面边缘之间的空白区域。 正确处理这些内容可避免文本被截断,确保不同打印机上的版式一致,并满足发票、报告及法律文件的排版要求。

IronPrint的Margins类接受以毫米为单位的值,并提供三个构造函数重载——统一的、水平/垂直的和按边设置的——因此我们可以在单行中满足任何布局要求。下面我们分别从安装到自定义边距打印逐一讲解每种方法。

快速入门:设置纸张边距
  1. 通过NuGet安装IronPrint: Install-Package IronPrint
  2. 添加using IronPrint;到文件
  3. 创建一个PrintSettings对象
  4. 将PaperMargins (值以毫米为单位)
  5. 将设置与文件路径一起传递给Printer.Print()
  1. 1Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2复制并运行这段代码。

    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);
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPrint
    arrow pointer

如何设置四边等宽页边距?

最简单的构造函数接受一个整数,并将其均匀应用于所有四边。 我们将值以毫米为单位传递:

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);

Left, Top, Right, 和Bottom分别设置为20毫米。 这是标准商业文档中最常见的选择,此类文档仅需在每行两端保持一致的空格即可。

IronPrint以毫米为单位测量边距,这避免了使用百分之一英寸的System.Drawing.Printing.Margins类的混淆。 IronPrint中的25.4毫米边距等同于new System.Drawing.Printing.Margins(100)——我们无需进行转换计算。

如何为每一侧设置不同的页边距?

当文档顶部需要额外空间放置页眉,或底部需要额外空间放置页脚时,我们使用四参数构造函数:

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);

参数顺序为left, top, right, bottom。 每个数值都是独立的,因此我们可以创建非对称布局,以适应页眉、页脚、装订边缘或打孔区域。 Margins类API参考记录了每个字段。

常见的页边距布局有哪些简写选项?

IronPrint的Margins类提供两种额外的构造函数:

水平/垂直简写 - Margins(int horizontal, int vertical)将左+右设置为第一个值,上+下设置为第二个:

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);

零边距 - Margins.Zero去除所有边距以实现无边框打印:

using IronPrint;

// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
    PaperMargins = Margins.Zero
};

// Print the poster
Printer.Print("poster.png", borderless);

请注意,大多数实体打印机都有硬件规定的最小可打印区域。 设置Margins.Zero会向驱动程序发送零边距指令,但根据打印机的功能,它可能依然会剪切靠近边缘的内容。

如何将页边距与其他打印设置结合使用?

PaperMargins是PrintSettings上的一个属性。 我们可以将纸张尺寸、方向、DPI、份数、灰度模式和打印机选择整合到一个配置对象中:

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);

对于异步工作流——WPF、MAUI 或ASP.NET web应用程序——用Printer.Print()以避免阻塞UI线程。 相同的PrintSettings对象适用于这两种方法。

我的下一个步骤是什么?

我们介绍了使用IronPrint配置打印边距的四种方法:使用Margins.Zero的无边框打印。 每种方法都通过Printer.PrintAsync()。

欲了解更多信息,请探索以下资源:

获取免费试用许可证,在真实环境中测试所有功能;或准备部署时查看授权选项。

常见问题解答

什么是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应用程序无缝集成,允许您在广泛的项目和工作流中合并自定义打印边距设置。

How do I print documents asynchronously with IronPrint?

For asynchronous printing, replace Printer.Print() with await Printer.PrintAsync() in your IronPrint-based workflow to prevent blocking the UI thread.

Where can I find further resources and tutorials for IronPrint?

Further resources for IronPrint can be found on the IronPrint tutorials page, API references for the Margins and Printer classes, and the Print Settings How-To guide on their website.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

Nuget Downloads 46,787版本:2026.10刚刚发布