如何在 C# 中指定打印机名称
IronPrint 的 PrintSettings 类提供了一个 PrinterName 属性,用于将打印作业定向到特定打印机。 我们将目标打印机的确切名称作为字符串赋值,将配置好的 PrintSettings 对象传递给 IronPrint 的任意打印方法,文档便会发送到该打印机而非系统默认打印机。
本指南将逐步介绍如何设置打印机名称、在运行时发现可用打印机,以及将打印机选择与其他打印设置相结合。
快速入门:指定打印机名称
- 通过 NuGet 安装 IronPrint:
Install-Package IronPrint - 在文件中添加
using IronPrint; - 创建一个
PrintSettings对象 - 将
PrinterName设置为目标打印机的确切名称 - 将设置传递给
Printer.Print()或Printer.PrintAsync()
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
PrinterName设置为目标打印机的名称 - 将设置传递给
Printer.Print() - 运行该项目以在指定打印机上打印
如何在 C# 中指定打印机名称?
我们通过将目标打印机的名称赋值给 PrintSettings 对象的 PrinterName 属性来指定目标打印机。 然后我们将该对象传递给 Printer.Print。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-set-printer-name.cs
using IronPrint;
// Configure print settings with a target printer
PrintSettings settings = new PrintSettings();
settings.PrinterName = "Microsoft Print to PDF";
// Send the document to the specified printer
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' Configure print settings with a target printer
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
' Send the document to the specified printer
Printer.Print("invoice.pdf", settings)
我们首先实例化 PrintSettings,该实例通过 PrinterName = null 进行初始化——即操作系统默认打印机。 随后,我们将 PrinterName 替换为目标打印机的确切名称。 当我们调用 Printer.Print 时,IronPrint 会将打印任务直接发送到该打印机的队列中。
有两点关键细节需特别注意。首先,打印机名称必须与操作系统报告的名称完全一致——此处区分大小写。若出现不匹配的情况(例如使用 "hp laserjet" 代替 "HP LaserJet"),系统将无提示地失败或抛出错误。 其次,如果用户通过 ShowPrintDialog 打开打印对话框,对话框中的选择将覆盖代码中设置的任何 PrinterName 值。 这是设计使然——对话框赋予用户最终控制权。
如何查找可用的打印机?
与其硬编码打印机名称,我们可以在运行时使用 Printer.GetPrinterNames() 查询系统。 此方法返回一个 List<string>,其中包含该机器上已安装的所有打印机。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-discover-printers.cs
using IronPrint;
// Discover all installed printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Use the first available printer
if (printers.Count > 0)
{
Printer.Print("report.pdf", new PrintSettings
{
PrinterName = printers[0]
});
}
Imports IronPrint
' Discover all installed printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Use the first available printer
If printers.Count > 0 Then
Printer.Print("report.pdf", New PrintSettings With {
.PrinterName = printers(0)
})
End If
我们调用 GetPrinterNames() 来获取操作系统所识别的所有打印机——包括本地打印机、网络打印机以及"Microsoft Print to PDF"等虚拟打印机。随后,我们遍历该列表,并通过索引、名称匹配或应用程序所需的任何自定义逻辑来选择打印机。
这种"先发现后打印"的模式对于部署在不同机器上的应用程序至关重要。 在单机环境下,硬编码打印机名称是可行的,但在生产环境中,应用程序应查询可用打印机,并让用户选择,或根据命名约定通过编程方式自动选择。 如需查看具体的代码示例,请参阅"获取打印机名称"示例。
如何将打印机名称与其他打印文档设置结合使用?
PrintSettings 类提供了 PrinterName 以及纸张尺寸、方向、DPI、边距、复印份数和扁平化等属性。 我们将所有配置集中在一个对象中。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-combined-settings.cs
using IronPrint;
// Build a fully configured print job targeting a specific printer
PrintSettings settings = new PrintSettings
{
PrinterName = "Office Color Printer",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
// Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings);
Imports IronPrint
' Build a fully configured print job targeting a specific printer
Dim settings As New PrintSettings With {
.PrinterName = "Office Color Printer",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.PaperMargins = New Margins(15, 15, 15, 15),
.Grayscale = False
}
' Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings)
我们采用对象初始化语法以提高可读性。 PrinterName 将任务路由至"Office Color Printer",其余属性则用于控制输出格式。 Dpi 300 分辨率下可呈现清晰的文本和图形。 PaperMargins 通过 Margins 构造函数接受四个毫米值——top、right、bottom、left。
IronPrint 会将配置作为整体进行验证,并将合并后的设置作为单个打印任务提交给打印机驱动程序。 有关托盘选择和灰度模式等其他选项,请参阅完整的打印设置指南。
如何选择打印机并进行异步打印?
对于无法阻塞主线程的应用程序(例如 WPF 或 WinForms 应用程序),我们使用 Printer.GetPrinterNamesAsync() 和 Printer.PrintAsync()。 两者均返回 Task,从而保持用户界面的响应性。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-async-printer-select.cs
using IronPrint;
using System.Threading.Tasks;
public class PrintService
{
public async Task PrintToFirstAvailableAsync(string filePath)
{
// Discover printers without blocking the UI
List<string> printers = await Printer.GetPrinterNamesAsync();
if (printers.Count == 0)
{
Console.WriteLine("No printers found.");
return;
}
// Configure and print to the first available printer
PrintSettings settings = new PrintSettings
{
PrinterName = printers[0],
Dpi = 300
};
await Printer.PrintAsync(filePath, settings);
}
}
Imports IronPrint
Imports System.Threading.Tasks
Public Class PrintService
Public Async Function PrintToFirstAvailableAsync(filePath As String) As Task
' Discover printers without blocking the UI
Dim printers As List(Of String) = Await Printer.GetPrinterNamesAsync()
If printers.Count = 0 Then
Console.WriteLine("No printers found.")
Return
End If
' Configure and print to the first available printer
Dim settings As New PrintSettings With {
.PrinterName = printers(0),
.Dpi = 300
}
Await Printer.PrintAsync(filePath, settings)
End Function
End Class
此基于类的示例将"发现并打印"逻辑封装在一个可重用的服务中。我们调用 GetPrinterNamesAsync() 来获取打印机列表,同时避免冻结用户界面,然后将第一个可用的打印机分配给 PrinterName。 await Printer.PrintAsync 调用会异步发送该任务。
在实际生产环境中,我们可能会用符合命名规范的逻辑替换 printers[0] ——例如,为运输标签搜索名称中包含"Label"的打印机,或为品牌文档搜索名称中包含"Color"的打印机。 IronPrint 的所有异步方法均接受相同的 PrintSettings 对象,因此 PrinterName 同步和异步路径的行为完全一致。
我的下一个步骤是什么?
我们介绍了如何在 C# 中使用 IronPrint 的 PrintSettings.PrinterName 属性指定打印机名称,内容涵盖从静态分配到使用 Printer.GetPrinterNames() 进行动态运行时检测。 关键要点:打印机名称必须完全一致(区分大小写),null 默认使用操作系统默认打印机,且打印对话框会覆盖程序化的打印机选择。
继续探索 IronPrint 的功能:
- 请参阅获取打印机名称的代码示例,了解独立的代码片段
- 查看完整的打印设置配置指南,了解所有可用属性
- 请查阅 Printer 类的 API 参考文档,了解
ShowPrintDialog等方法以及托盘管理功能 - 浏览
PaperSize和Margins的完整 API 参考文档
立即开始 30 天试用,在您的项目中测试打印机选择功能,或查看用于生产部署的许可选项。

