如何在 C# 中设置打印份数
打印多个副本是常规需求 - 需要原件和副本的发票、批量打印的运输标签或分发给多个部门的报告。 而不是在循环中调用Print()并为每个副本创建单独的打印任务,正确的方法是一次性设置副本数量,并让打印机驱动程序在一个作业中处理复制。
IronPrint的PrintSettings.NumberOfCopies属性接受一个整数,并在一个操作中将指定数量的副本发送到打印机。 我们在下面介绍安装、基本用法和组合设置。
- 通过NuGet安装IronPrint:
Install-Package IronPrint - 将
using IronPrint;添加到文件中 - 创建一个
PrintSettings对象 - 将
NumberOfCopies设置为所需的数量 - 通过文件路径将设置传递给
Printer.Print()
-
1使用 IronPrint 安装 NuGet Package Manager
-
2复制并运行这段代码。
using IronPrint; // Print 3 copies of a PDF in one print job PrintSettings settings = new PrintSettings(); settings.NumberOfCopies = 3; Printer.Print("invoice.pdf", settings);Imports IronPrint ' Print 3 copies of a PDF in one print job Dim settings As New PrintSettings() settings.NumberOfCopies = 3 Printer.Print("invoice.pdf", settings)Imports IronPrint ' Print 3 copies of a PDF in one print job Dim settings As New PrintSettings() settings.NumberOfCopies = 3 Printer.Print("invoice.pdf", settings) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
NumberOfCopies设置为所需数量 - 将设置传递给
Printer.Print() - 验证打印机是否输出正确份数的副本
如何设置静默打印的副本数量?
静默打印会将文档直接发送至打印机,而不会显示任何对话框。 通过PrintSettings.NumberOfCopies配置副本数量,并将设置传递给Printer.Print():
using IronPrint;
// Configure the print job for 5 copies
PrintSettings settings = new PrintSettings
{
NumberOfCopies = 5
};
// Print the shipping label
Printer.Print("shipping-label.pdf", settings);using IronPrint;
// Configure the print job for 5 copies
PrintSettings settings = new PrintSettings
{
NumberOfCopies = 5
};
// Print the shipping label
Printer.Print("shipping-label.pdf", settings);Imports IronPrint
' Configure the print job for 5 copies
Dim settings As New PrintSettings With {
.NumberOfCopies = 5
}
' Print the shipping label
Printer.Print("shipping-label.pdf", settings)Imports IronPrint
' Configure the print job for 5 copies
Dim settings As New PrintSettings With {
.NumberOfCopies = 5
}
' Print the shipping label
Printer.Print("shipping-label.pdf", settings)打印机驱动程序在硬件层级接收复制指令,这比将五个单独的任务放入队列更快、更可靠。 这一点在共享网络打印机上尤为重要,因为不同的打印任务可能会与其他用户的文档交错打印。
PrintSettings.NumberOfCopies在未显式设置时默认为1。 接受任何正整数 - 将其设置为0或负值没有实际效果,驱动程序会回到单个副本。
如何将“副本数量”与其他打印设置结合使用?
PrintSettings.NumberOfCopies是PrintSettings上的一个属性。 我们可以在一个配置对象中将其与纸张边距、纸张尺寸、方向、DPI、灰度模式和打印机选择结合使用:
using IronPrint;
// Configure 3 copies with landscape A4 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"
};
// Print the Q4 report
Printer.Print("Q4-report.pdf", settings);using IronPrint;
// Configure 3 copies with landscape A4 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"
};
// Print the Q4 report
Printer.Print("Q4-report.pdf", settings);Imports IronPrint
' Configure 3 copies with landscape A4 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"
}
' Print the Q4 report
Printer.Print("Q4-report.pdf", settings)Imports IronPrint
' Configure 3 copies with landscape A4 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"
}
' Print the Q4 report
Printer.Print("Q4-report.pdf", settings)对于非阻塞工作流,请将同一PrintSettings传递给Printer.PrintAsync()。
字数限制是否受平台影响?
在某些平台上,打印机驱动程序可能无法再现PrintSettings.NumberOfCopies中指定的确切数量。 IronPrint文档指出,平台特定的限制可能导致该值被忽略,导致只打印一份。 这是驱动程序级别的限制,并非 IronPrint 的局限性。
在Windows桌面应用程序中 - 大多数打印工作流程的主要目标 - PrintSettings.NumberOfCopies在本地和网络打印机上都能可靠地执行。 如果遇到打印机始终忽略该设置,请通过 Windows 打印机属性面板确认其驱动程序是否支持按页排序的多份打印任务。
我的下一个步骤是什么?
我们介绍了如何使用PrintSettings.NumberOfCopies设置打印副本的数量,演示了静默和异步打印,将副本数量与其他设置结合使用,并注意了平台特定的注意事项。
欲了解更多信息,请探索以下资源:
- IronPrint教程 - 打印文档 用于端到端的打印演练。
- 打印设置指南包括边距、DPI、方向等。
- PrintSettings 类API参考 提供完整的属性文档。
- 打印机类API参考 提供所有静态打印方法。
获取免费试用许可证 以在真实环境中测试所有功能,或在准备好部署时查看许可选项。
常见问题
如何使用IronPrint在C#中打印多份副本?
要使用IronPrint在C#中打印多份副本,请创建一个PrintSettings对象并将NumberOfCopies属性设置为所需数量。然后,将这些设置与文件路径一起传递给Printer.Print()方法。
使用IronPrint的NumberOfCopies属性有什么好处?
使用IronPrint的NumberOfCopies属性很高效,因为它允许打印机驱动程序在一次操作中管理副本,避免了每份副本都需循环多个打印作业的问题。
我可以使用IronPrint为静默打印设置副本数量吗?
是的,您可以在IronPrint中通过配置PrintSettings.NumberOfCopies并将其传递给Printer.Print()来设置静默打印的副本数量。
如何使用IronPrint组合多个打印设置?
IronPrint允许您将多个设置组合在一个PrintSettings配置对象内,如NumberOfCopies、纸张大小、方向、DPI和打印机选择。
设置IronPrint的副本数量时是否有平台限制?
是的,由于打印机驱动程序限制,某些平台可能不会遵守指定的NumberOfCopies,尤其是当驱动程序不支持成册多副本作业时。
如果在IronPrint中将NumberOfCopies设置为0或负值会发生什么?
将NumberOfCopies设置为0或负值不会产生实质性效果,因为打印机驱动程序会默认为打印一份副本。
如何在IronPrint中进行多份副本的异步打印?
您可以通过在IronPrint中使用相同的PrintSettings对象与Printer.PrintAsync()方法实现异步打印。
使用IronPrint的最佳操作系统是什么?
IronPrint与Windows桌面应用程序结合良好,NumberOfCopies属性在本地和网络打印机中均得到可靠支持。
如何确保我的打印机支持多副本功能?
要确保您的打印机支持多副本功能,请通过Windows打印机属性面板检查其驱动程序属性中是否支持成册多副本作业。
我在哪里可以找到更多的IronPrint详细文档和教程?
您可以在Iron Software网站上查看IronPrint教程和API参考,以获取详细文档和完整的打印演练。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。