如何在 C# 中使用对话框进行打印

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

打印对话框是操作系统中的标准窗口,允许用户在打印前选择打印机、设置份数、选择页码范围以及调整纸张选项。 对于用户需要控制文档打印方式和位置的桌面应用程序,显示此对话框是预期行为。

IronPrint 提供了一个方法 —— Printer.ShowPrintDialog() —— 该方法会显示原生打印对话框,应用用户选择,并将文档发送至选定的打印机。 我们只需安装一个 NuGet 包,并编写一行代码。

快速入门:使用对话框进行打印

  1. 通过 NuGet 安装 IronPrint:Install-Package IronPrint
  2. 在文件中添加 using IronPrint;
  3. 调用 Printer.ShowPrintDialog("filepath") 以打开原生打印对话框
  4. (可选)传递一个 PrintSettings 对象以预配置对话框默认值
  5. 用户选择打印机、复印份数和选项,然后点击"打印"
  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 复制并运行这段代码。

    using IronPrint;
    
    // Display the print dialog and print the document
    Printer.ShowPrintDialog("document.pdf");
  3. 部署到您的生产环境中进行测试

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

    arrow pointer

C# 中的打印对话框如何工作?

Printer.ShowPrintDialog() 方法将打开操作系统的原生打印对话框。 用户将看到完整的打印选项——打印机选择、份数、页码范围、纸张方向和纸张尺寸——并点击"打印"发送任务,或点击"取消"关闭对话框而不进行打印。

在底层实现中,原生 .NET 方法需要创建一个 System.Windows.Forms.PrintDialog 实例,将其与 PrintDocument 关联,处理 PrintPage 事件以将内容绘制到打印图形表面,检查 DialogResult,然后调用 PrintDocument.Print()。 该配置通常需要 15 至 25 行代码。 此外,它不包含内置的 PDF 或图像渲染功能——通过原生对话框打印 PDF 意味着首先需将 PDF 解析为可绘制的页面,这需要使用另一个库。

IronPrint 通过一次调用即可处理整个流程:

:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-show-print-dialog-open-and-print.cs
using IronPrint;

// Open the dialog, let the user configure settings, and print
Printer.ShowPrintDialog("quarterly-report.pdf");
Imports IronPrint

' Open the dialog, let the user configure settings, and print
Printer.ShowPrintDialog("quarterly-report.pdf")
$vbLabelText   $csharpLabel

该方法接受文件路径作为 string 或原始文件数据作为 byte[]。 IronPrint 会检测文档格式,通过相应的引擎进行渲染,并显示对话框。 用户确认后,文档将按其选择的设置进行打印。 打印文档教程将更详细地介绍完整的打印生命周期。

如何预配置对话框设置?

我们可以通过创建一个 PrintSettings 对象并将其作为第二个参数传递,在对话框打开前设置默认值。 对话框打开时将预先选中这些值,用户可以直接接受,也可以覆盖任何设置。

:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-preconfigure-dialog-settings.cs
using IronPrint;

// Pre-configure defaults for the dialog
var settings = new PrintSettings
{
    PrinterName = "HP LaserJet Pro",
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300,
    NumberOfCopies = 2,
    Grayscale = false
};

// Open dialog with pre-filled settings
Printer.ShowPrintDialog("invoice.pdf", settings);
Imports IronPrint

' Pre-configure defaults for the dialog
Dim settings As New PrintSettings With {
    .PrinterName = "HP LaserJet Pro",
    .PaperSize = PaperSize.A4,
    .PaperOrientation = PaperOrientation.Portrait,
    .Dpi = 300,
    .NumberOfCopies = 2,
    .Grayscale = False
}

' Open dialog with pre-filled settings
Printer.ShowPrintDialog("invoice.pdf", settings)
$vbLabelText   $csharpLabel

当应用程序能预先知晓可能使用的打印机或纸张规格时,此功能非常有用。 例如,一个始终使用特定热敏打印机打印收据的销售点系统,可以默认 PrinterName 设置为该设备。用户仍可在对话框中选择更改该设置。

要查询系统中可用的打印机,我们调用 Printer.GetPrinterNames() 方法,该方法将返回一个包含所有已安装打印机的 List<string> 数组。 同样地,Printer.GetPrinterTrays() 返回指定打印机可用的纸盒。

可配置属性的完整列表包括 Flatten(用于 PDF 表单字段)以及 Tray打印设置指南通过代码示例详细介绍了每个属性。 PrintSettings 中未设置的任何属性将默认采用所选打印机的标准配置。

如何异步显示对话框?

Printer.ShowPrintDialogAsync() 方法返回 Task,使其与 await 兼容。 这可防止对话框阻塞 UI 线程——这对 WPF、MAUI 以及任何因界面冻结而导致用户体验不佳的应用程序而言至关重要。

:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-show-print-dialog-async.cs
using IronPrint;

// Non-blocking dialog — the UI remains responsive
await Printer.ShowPrintDialogAsync("report.pdf");
Imports IronPrint

' Non-blocking dialog — the UI remains responsive
Await Printer.ShowPrintDialogAsync("report.pdf")
$vbLabelText   $csharpLabel

ShowPrintDialogAsync() 接受与同步版本相同的参数:文件路径或字节数组,以及一个可选的 PrintSettings 对象。 异步模式遵循现代 .NET 开发中通用的基于 Task 的异步模式

IronPrint 支持 WinForms、WPF、MAUI 及控制台应用程序。 对话框的外观会根据宿主平台和操作系统版本进行适配,因此用户始终能看到其预期的原生打印窗口。

何时应使用对话框打印,何时应使用静默打印?

选择取决于用户是否需要控制打印任务。

标准 带对话框的打印 无声印刷
用户交互 用户选择打印机、复印、页码范围 无需交互——立即打印
最适合 桌面应用、单次打印、面向用户的功能 批处理作业、后台服务、自助终端应用
打印机选择 用户通过对话框进行选择 通过 PrintSettings 进行程序化设置
IronPrint 方法 Printer.ShowPrintDialog() Printer.Print()
异步版本 ShowPrintDialogAsync() PrintAsync()

当用户在打印前(如导出报表、打印发票,或任何因选错打印机导致纸张浪费的场景)需要验证或更改设置时,请使用此对话框。 当应用程序完全控制打印任务且无需人工干预时,请使用静默打印打印文档教程并排展示了这两种方法。

打印PDF时对话框支持哪些文件格式?

Printer.ShowPrintDialog() 支持与静默打印相同的格式:PDF、PNG、TIFF、JPEG、GIF、HTML 和 BMP。无论文件格式如何,我们都会传递文件路径,而 IronPrint 负责处理渲染及与打印队列的通信。 也接受以 byte[] 格式呈现的文件数据,这在文档于内存中生成或从数据库检索时尤为有用。

:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-print-dialog-image-and-byte-array.cs
using IronPrint;

// Print an image with the dialog
Printer.ShowPrintDialog("product-photo.png");

// Print from a byte array
byte[] reportData = File.ReadAllBytes("monthly-report.pdf");
Printer.ShowPrintDialog(reportData);
Imports IronPrint

' Print an image with the dialog
Printer.ShowPrintDialog("product-photo.png")

' Print from a byte array
Dim reportData As Byte() = File.ReadAllBytes("monthly-report.pdf")
Printer.ShowPrintDialog(reportData)
$vbLabelText   $csharpLabel

代码示例页面展示了更多特定于不同格式的应用场景。 对于生成 PDF 并立即打印的特定工作流,IronPDF 与 IronPrint 堪称天作之合。 IronPrint 与 IronPDF 的对比说明了何时应使用哪一款。

下一步

使用对话框进行打印主要有两种方法:Printer.ShowPrintDialog() 用于同步调用,Printer.ShowPrintDialogAsync() 用于非阻塞执行。 使用 PrintSettings 预设默认值,并允许用户在此基础上进行调整。 这两种方法均支持 IronPrint 的所有文档格式,并适用于 WinForms、WPF、MAUI 和控制台项目。

请查阅 IronPrint 教程以获取完整操作指南,查阅 Printer 类 API 参考以了解所有可用方法,或查阅打印设置指南以进行高级配置。 更新日志记录了最近的改进和新功能。

立即开始 30天试用,在实际项目中测试对话框打印功能——无需信用卡。 准备发布时,请查看起价为 749 美元的许可选项

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 38,930 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPrint
运行示例 观看您的文档打到打印机上。