IRONSOFTWAREHOME

如何在 C# 中指定打印机名称

Curtis Chau
Curtis Chau
Updated: 2026年5月9日

IronPrint的PrinterName属性,用于将打印任务定向到特定的打印机。 我们将目标打印机的确切名称作为字符串分配,将配置好的PrintSettings对象传递给任何IronPrint打印方法,文档就会被发送到该打印机而不是系统默认的打印机。

本指南将逐步介绍如何设置打印机名称、在运行时发现可用打印机,以及将打印机选择与其他打印设置相结合。

快速入门:指定打印机名称
  1. 1Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

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

    using IronPrint;
    
    // Print a document to a specific printer
    Printer.Print("report.pdf", new PrintSettings
    {
        PrinterName = "HP LaserJet Pro M404"
    });
    C#
  3. 3部署到您的生产环境中进行测试

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

如何在 C# 中指定打印机名称?

我们通过将目标打印机名称分配给PrinterName属性来指定目标打印机。 然后我们将该对象传递给Printer.Print

输入

invoice.pdf是一份带有项目和总计的格式化A4 B2B发票,通过PrintSettings.PrinterName打印到特定命名的打印机。

using IronPrint;

// Configure print settings with a target printer
PrintSettings settings = new PrintSettings();
settings.PrinterName = "Microsoft Print to PDF";

// Print to the specified printer
Printer.Print("invoice.pdf", settings);

我们首先实例化PrinterName = null初始化, 这意味着操作系统默认打印机。 然后我们用目标打印机的确切字符串名称覆盖PrinterName。 当我们调用Printer.Print时,IronPrint会将任务直接发送到该打印机的队列。

输出

发票被直接发送到命名的打印机。 作业如静默打印示例中所示出现在打印机队列中。

发票输出使用打印机名称打印到"Microsoft Print to PDF"。

请记住两个细节。 首先,打印机名称必须与操作系统报告的完全匹配; 这个比较是区分大小写的。像"HP LaserJet"这样的不匹配将默默失败或抛出错误。 其次,如果用户通过PrinterName,给予用户最终的控制权。

如果不知道打印机名称,则可以在运行时查询所有已安装的打印机,而不是硬编码该值。

如何查找可用的打印机?

而不是硬编码打印机名称,我们可以在运行时使用Printer.GetPrinterNames查询系统。 这种方法返回一个List<string>,其中包含机器上安装的每一个打印机。

输入

report.pdf是一个带有摘要部分和数据表的A4业务报告,打印到第一个动态发现的打印机。

using IronPrint;

// List every installed printer
List<string> printers = Printer.GetPrinterNames();

// Print each printer name
foreach (string name in printers)
{
    Console.WriteLine(name);
}

// Print to the first available printer
if (printers.Count > 0)
{
    Printer.Print("report.pdf", new PrintSettings
    {
        PrinterName = printers[0]
    });
}

我们调用GetPrinterNames以检索操作系统知道的每台打印机,包括本地的、网络的和虚拟打印机如"Microsoft Print to PDF"。然后我们遍历列表并通过索引、名称匹配或应用程序需要的任何自定义逻辑选择打印机。

输出

报告被发送到第一个可用的打印机。 作业如上所示出现在打印机队列中。

报告输出通过GetPrinterNames打印到第一个可用的打印机。

这种"先发现后打印"的模式对于部署在不同机器上的应用程序至关重要。 在单机环境下,硬编码打印机名称是可行的,但在生产环境中,应用程序应查询可用打印机,并让用户选择,或根据命名约定通过编程方式自动选择。

打印机选择还可以与纸张大小、DPI、复制次数和边距结合在一个配置对象中。

如何将打印机名称与其他设置结合使用?

PrinterName之外,还公开了纸张尺寸、方向、DPI、边距、副本数等属性。 我们将所有配置集中在一个对象中。

输入

quarterly-report.pdf是带有KPI卡片和图表的A4季度财务报告,与PrintSettings对象中打印。

using IronPrint;

// Configure full print settings with a named 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 the quarterly report
Printer.Print("quarterly-report.pdf", settings);

我们采用对象初始化语法以提高可读性。 PrinterName将任务路由到"Office Color Printer",而其余属性控制输出格式。 Dpi在300时产生清晰的文本和图形。 Margins构造函数接受毫米值。 有关所有可用属性的完整列表,请参阅打印设置指南

输出

季度报告打印使用命名打印机,A4竖版,300 DPI,2份,15毫米边距结合在一个作业中。 作业如上所示出现在打印机队列中。

季度报告输出包括PrinterName、A4、300 DPI、2份和15毫米边距。

IronPrint 会将配置作为整体进行验证,并将合并后的设置作为单个打印任务提交给打印机驱动程序。

不能在打印机发现或打印期间阻塞主线程的应用程序可以使用两个操作的异步等效版本。

如何选择打印机并进行异步打印?

对于WPF和WinForms应用程序,其中阻塞主线程不可选,我们使用Printer.PrintAsync。 两者都返回Task,保持UI响应。

using IronPrint;
using System.Threading.Tasks;

public class PrintService
{
    public async Task PrintToFirstAvailableAsync(string filePath)
    {
        // Discover available printers asynchronously
        List<string> printers = await Printer.GetPrinterNamesAsync();

        // Exit if no printers are installed
        if (printers.Count == 0)
        {
            Console.WriteLine("No printers found.");
            return;
        }

        // Configure settings for the first available printer
        PrintSettings settings = new PrintSettings
        {
            PrinterName = printers[0],
            Dpi = 300
        };

        // Print asynchronously
        await Printer.PrintAsync(filePath, settings);
    }
}

这个基于类的示例将发现和打印逻辑封装在一个可重用服务中。我们调用PrinterNameawait Printer.PrintAsync调用异步发送任务。

输出

发票异步打印到第一个发现的打印机。 作业如上所示出现在打印机队列中。

发票输出异步打印到第一个可用的打印机。

在生产中,我们可能会用匹配命名约定的逻辑替换printers[0],例如搜索包含"Label"用于运输标签或"Color"用于品牌文档的打印机。 IronPrint的异步方法都接受相同的PrinterName行为相同。

涵盖了静态分配、运行时发现、组合设置和异步工作流程之后,一些相关资源值得收藏。

我的下一个步骤是什么?

我们以Printer.GetPrinterNames的动态运行时发现。 关键点:打印机名称必须完全匹配(区分大小写),null默认使用操作系统默认打印机,并且打印对话框覆盖程序选择。

获取打印机名称示例提供独立的发现片段,而打印设置指南详细介绍每个可配置属性。 打印机类API参考文档记录所有静态打印方法,PrintSettings API参考列出每个字段和默认值。 有关端到端的演练,打印文档教程将一切结合在一起。

开始免费30天试用在实际项目中测试打印机选择。 准备好后,查看许可选项,起始于$999。

https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.PrintSettings.html https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.PrintSettings.html https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html https://ironsoftware.com/csharp/print/how-to/print-settings/https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html

常见问题解答

如何使用IronPrint在C#中指定打印机名称?

通过IronPrint,您可以通过设置PrinterName属性在C#中指定打印机名称。这样可以使您针对打印任务选择特定的打印机。

什么是 IronPrint?

IronPrint是Iron Software开发的一款库,能够让开发人员管理C#应用程序中的打印任务,提供指定打印机名称和其他打印选项的功能。

如何在C#中发现可用的打印机?

使用IronPrint,您可以通过访问打印机列表轻松发现网络中的可用打印机,并为您的打印任务选择所需的PrinterName。

我可以动态设置PrinterName属性吗?

是的,使用IronPrint,您可以在C#代码中动态设置PrinterName属性,以在运行时选择所需的打印机。

使用IronPrint可以打印到网络打印机吗?

是的,IronPrint允许您通过在C#应用程序中设置适当的PrinterName属性来指定和打印到网络打印机。

Is it possible to print asynchronously with IronPrint?

Yes, you can use `Printer.GetPrinterNamesAsync` and `Printer.PrintAsync` methods in IronPrint to perform asynchronous printing, which is useful for keeping UI applications responsive.

Does IronPrint support user interaction via print dialogs?

Yes, in IronPrint, if a print dialog is opened using `ShowPrintDialog`, it can override the programmatic selection in the `PrinterName` property, giving end-users final control over printer selection.

What should I do if no printers are found using IronPrint?

If no printers are found with IronPrint's `Printer.GetPrinterNames`, ensure that printers are correctly installed and available on the system. You may also execute fallback logic or notify the user.

How do I ensure sharp print output using IronPrint?

To ensure sharp print output with IronPrint, set the `Dpi` property in the `PrintSettings` object to a suitable dpi value like 300, which produces high-quality text and graphics.

Can IronPrint be used for network and virtual printers?

Yes, IronPrint supports printing to local, network, and virtual printers, like 'Microsoft Print to PDF', as long as they are recognized by the operating system.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 46,090版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPrint
nuget.org/packages/IronPrint/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择“浏览”并搜索“IronPrint”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并解压 IronPrint 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择“浏览”,“IronPrint.dll”

$999 起售

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户