如何在 PDF 中使用 C# 配置打印设置 | IronPrint

如何使用 IronPrint 在 C# 中配置打印设置

This article was translated from English: Does it need improvement?
Translated
View the article in English

打印设置是指决定如何打印文档或内容的配置或参数集。 这些设置包括纸张尺寸、方向(例如,纵向或横向)、打印分辨率(每英寸点数 - DPI)、份数、打印机选择、边距以及灰度打印等选项等详细信息。 用户可以自定义这些设置,以满足特定的打印偏好和要求。

快速入门:使用 IronPrint 设置进行配置和打印

只需创建一个PrintSettings对象,并设置一些属性(例如纸张尺寸、方向、DPI、份数和灰度),即可轻松上手。 然后调用Printer.Print(...)即可立即应用这些设置——无需复杂的设置。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronPrint

    PM > Install-Package IronPrint

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

    IronPrint.Printer.Print("document.pdf", new IronPrint.PrintSettings { PaperSize = IronPrint.PaperSize.A4, PaperOrientation = IronPrint.PaperOrientation.Landscape, Dpi = 300, NumberOfCopies = 2, Grayscale = true });
  3. 部署到您的生产环境中进行测试

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

设置打印设置

要配置打印设置,请实例化 PrintSettings 类并根据您的喜好进行配置。 在PrintShowPrintDialog方法中,将 PrintSettings 对象作为第二个参数传递。 下面的代码示例说明了这种用法。

// Import the necessary namespace for IronPrint
using IronPrint;

// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();

// Configure various print settings
settings.PaperSize = PaperSize.A4;                // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300;                               // Set print resolution to 300 DPI
settings.NumberOfCopies = 2;                      // Set the number of copies to 2
settings.PrinterName = "MyPrinter";               // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true;                        // Print in grayscale

// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
// Import the necessary namespace for IronPrint
using IronPrint;

// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();

// Configure various print settings
settings.PaperSize = PaperSize.A4;                // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300;                               // Set print resolution to 300 DPI
settings.NumberOfCopies = 2;                      // Set the number of copies to 2
settings.PrinterName = "MyPrinter";               // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true;                        // Print in grayscale

// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
' Import the necessary namespace for IronPrint
Imports IronPrint

' Initialize a new instance of the PrintSettings class
Private settings As New PrintSettings()

' Configure various print settings
settings.PaperSize = PaperSize.A4 ' Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape ' Set paper orientation to Landscape
settings.Dpi = 300 ' Set print resolution to 300 DPI
settings.NumberOfCopies = 2 ' Set the number of copies to 2
settings.PrinterName = "MyPrinter" ' Set the name of the printer
settings.PaperMargins = New Margins(10, 10, 10, 10) ' Set margins to 10mm on each side
settings.Grayscale = True ' Print in grayscale

' Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings)
$vbLabelText   $csharpLabel

可用的打印设置

请查看以下所有可用的打印设置选项:

  • DefaultSettings :使用默认值初始化 IronPrint.PrintSettings 类的新实例。
  • PaperSize :设置打印机使用的纸张尺寸。 默认值为 IronPrint.PaperSize.PrinterDefault。
  • PaperOrientation :指定纸张方向(例如,纵向或横向)。 默认值为 IronPrint.PaperOrientation.Portrait。
  • Dpi :表示预期的打印分辨率,单位为每英寸点数。 默认值为 300,这是商业印刷中常用的设置。 备注:实际打印所用的DPI可能受限于打印机的性能。
  • NumberOfCopies :指示打印文档时要生成的相同副本的数量。 默认值为 1 份。 备注:在某些平台上,可能存在一些限制,导致无法准确复制多个副本。 在这种情况下,IronPrint.PrintSettings.NumberOfCopies 的指定值可能会被忽略,导致只打印一份副本。
  • PrinterName :指定用于打印的打印机的名称。 默认值为空,这意味着将使用操作系统当前的默认打印机。 备注:如果在打印对话框中选择打印机,则此设置将被忽略。 要获取可用的打印机名称,可以使用 IronPrint.Printer.GetPrinterNames 或 IronPrint.Printer.GetPrinterNamesAsync 来获取打印机名称列表。
  • PaperMargins :设置打印时使用的页边距(单位:毫米)。 默认值为空,表示使用打印机提供的默认边距。 -灰度:指示是否以灰度打印。 备注:默认值为 false,表示尝试彩色打印。 -展平:在打印前展平 PDF,这对于显示表单字段值和图像非常有用。 默认值为 false,表示 PDF 在打印前将被展平。 -纸盒:用于打印作业的打印机纸盒。 这样,用户可以指定要从哪个纸盒向打印机送纸。 如果在打印对话框中选择纸盒,则此设置将被忽略。 要获取可用纸盒,可以使用 IronPrint.Printer.GetPrinterTrays(System.String) 或 IronPrint.Printer.GetPrinterTraysAsync(System.String)。 默认值为空,表示使用打印机提供的默认纸盒。 此托盘选择属性仅在 Windows 系统中可用。

常见问题解答

我如何在 C# 中配置打印设置?

您可以使用 IronPrint 库在 C# 中配置打印设置。首先,实例化 PrintSettings 类并设置您想要的参数,如纸张尺寸、方向、DPI 等。然后,将配置好的 PrintSettings 对象传递给 PrintShowPrintDialog 方法。

有哪些可用的纸张尺寸可以打印?

IronPrint 允许您通过 PrintSettings 类的 PaperSize 属性设置各种纸张尺寸。默认情况下,它设置为 IronPrint.PaperSize.PrinterDefault,但可以自定义为 A4、Letter 等尺寸。

我可以更改打印文档的方向吗?

可以,您可以通过在 PrintSettings 类中设置 PaperOrientation 属性来更改方向。它支持 PortraitLandscape 方向。

如何调整打印分辨率(DPI)?

在 IronPrint 中,您可以通过在 PrintSettings 类中设置 Dpi 属性来调整打印分辨率。默认分辨率为 300 DPI。

使用 IronPrint 可以打印多份副本吗?

可以,通过在 PrintSettings 类中设置 NumberOfCopies 属性,您可以指定要打印的副本数量。请注意,某些平台可能对该功能有影响。

如何为打印任务选择特定的打印机?

要选择特定的打印机,请在 PrintSettings 类中设置 PrinterName 属性。如果您没有指定打印机,系统将使用默认打印机。

我可以配置打印输出的边距吗?

可以,您可以通过将 PrintSettings 类中的 PaperMargins 属性设置为所需的毫米值来配置打印边距。

如何以灰度打印文档?

要以灰度打印,将 PrintSettings 类中的 Grayscale 属性设置为 true。默认设置为 false,意图为彩色打印。

Flatten 属性有什么作用?

IronPrint 中的 Flatten 属性用于在打印之前展平 PDF。这个过程有助于确保表单字段和图像在打印输出中正确显示。

如何为打印任务指定打印机托盘?

您可以通过在 PrintSettings 类中设置 Tray 属性指定一个打印机托盘。但是,如果在 PrintDialog 中选择了托盘,则此属性将被忽略。IronPrint 提供了像 GetPrinterTrays 这样的方法来列出可用的托盘。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 34,704 | Version: 2025.11 刚刚发布