如何在 C# 中设置打印份数
打印文档的多份副本是一项常规需求——例如需要正本和副本的发票、批量打印的运输标签,或是分发给多个部门的报告。 与其在循环中调用 Print() 并为每份副本创建单独的打印任务,正确的做法是仅设置一次副本数量,由打印机驱动程序在单个任务中处理重复打印。
IronPrint 的 PrintSettings.NumberOfCopies 属性接受一个整数,并在一项操作中将指定数量的副本发送至打印机。 下文将涵盖安装、基本用法、异步工作流以及组合设置。
快速入门:设置副本数量
- 通过 NuGet 安装 IronPrint:
Install-Package IronPrint - 在文件中添加
using IronPrint; - 创建一个
PrintSettings对象 - 将
NumberOfCopies设置为所需数量 - 将设置与文件路径一并传递给
Printer.Print()
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
复制并运行这段代码。
using IronPrint; // Print 3 copies of a PDF in one print job PrintSettings settings = new PrintSettings(); settings.NumberOfCopies = 3; Printer.Print("invoice.pdf", settings); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
NumberOfCopies设置为所需数量 - 将设置传递给
Printer.Print() - 验证打印机是否输出正确份数的副本
如何设置静默打印文档的副本数量?
静默打印会将文档直接发送至打印机,而不会显示任何对话框。 我们通过 PrintSettings.NumberOfCopies 配置副本数量,并将设置传递给 Printer.Print():
:path=/static-assets/print/content-code-examples/how-to/set-number-of-copies/set-number-of-copies-silent-copies.cs
using IronPrint;
// Print 5 copies silently to the default printer
PrintSettings settings = new PrintSettings
{
NumberOfCopies = 5
};
Printer.Print("shipping-label.pdf", settings);
Imports IronPrint
' Print 5 copies silently to the default printer
Dim settings As New PrintSettings With {
.NumberOfCopies = 5
}
Printer.Print("shipping-label.pdf", settings)
打印机驱动程序在硬件层级接收复制指令,这比将五个单独的任务放入队列更快、更可靠。 这一点在共享网络打印机上尤为重要,因为不同的打印任务可能会与其他用户的文档交错打印。
NumberOfCopies 在未显式设置时默认值为 1。 接受任何正整数——将其设置为 0 或负值均无实际效果,驱动程序将回退到单份副本。
如何将"副本数量"与其他打印设置结合使用?
NumberOfCopies 是 PrintSettings 上的一个属性。 我们可以将纸张边距、纸张尺寸、纸张方向、DPI、灰度模式和打印机选择整合到一个配置对象中:
:path=/static-assets/print/content-code-examples/how-to/set-number-of-copies/set-number-of-copies-combined-settings.cs
using IronPrint;
// Full configuration: 3 copies of a landscape A4 report at 300 DPI
PrintSettings settings = new PrintSettings
{
NumberOfCopies = 3,
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape,
Dpi = 300,
PaperMargins = new Margins(15),
Grayscale = false,
PrinterName = "HP LaserJet Pro MFP M428"
};
Printer.Print("Q4-report.pdf", settings);
Imports IronPrint
' Full configuration: 3 copies of a landscape A4 report at 300 DPI
Dim settings As New PrintSettings With {
.NumberOfCopies = 3,
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape,
.Dpi = 300,
.PaperMargins = New Margins(15),
.Grayscale = False,
.PrinterName = "HP LaserJet Pro MFP M428"
}
Printer.Print("Q4-report.pdf", settings)
对于非阻塞工作流,请将 PrintSettings 替换为 Printer.PrintAsync()。
字数限制是否受平台影响?
在某些平台上,打印机驱动程序可能无法准确重现 NumberOfCopies 中指定的具体数字。 IronPrint 文档指出,受平台特有的限制,该值可能会被忽略,从而导致仅生成一份副本。 这是驱动程序级别的限制,并非 IronPrint 的局限性。
在 Windows 桌面应用程序中(这是大多数打印工作流的主要目标),NumberOfCopies 能够被本地和网络打印机可靠地识别。 如果遇到打印机始终忽略该设置,请通过 Windows 打印机属性面板确认其驱动程序是否支持按页排序的多份打印任务。
我的下一个步骤是什么?
我们介绍了如何使用 PrintSettings.NumberOfCopies 设置打印份数,演示了静默打印和异步打印,讲解了如何将份数与其他设置结合使用,并指出了各平台的特定注意事项。
欲了解更多信息,请探索以下资源:
- IronPrint 教程 — 文档打印,提供端到端打印操作指南。
- 打印设置指南:涵盖页边距、DPI、纸张方向等内容。
- 请参阅 PrintSettings 类 API 参考文档以获取完整的属性说明。
- 打印机类 API 参考文档,涵盖所有静态打印方法。

