如何在 C# 中使用对话框进行打印 Copy for LLMsCopy for LLMs Copy page as Markdown for LLMs
# 如何在 C# 中使用对话框进行打印
[IronPrint](https://ironsoftware.com/csharp/print/)提供`Printer.ShowPrintDialog()`:一种方法,用于打开本机操作系统打印对话框,捕获用户的打印机和纸张选择,并将文档发送到打印队列。 安装一个NuGet包并写一行代码。
打印对话框是标准的操作系统窗口,允许用户在打印前选择打印机、设置副本、选择页面范围和调整纸张选项。 需要在每个作业前给予用户控制权的桌面应用程序是主要的使用案例。
*as-heading:2(快速入门:使用对话框进行打印)*
```csharp
:title=Quickstart
using IronPrint;
// Display the print dialog and print the document
Printer.ShowPrintDialog("document.pdf");
```
<div class="hsg-featured-snippet">
<h3>最小工作流程(5 个步骤)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronPrint/">安装 IronPrint C# 打印库</a></li>
<li>调用 <code>Printer.ShowPrintDialog("filepath")</code></li>
<li>用户选择打印机、副本数和页码范围</li>
<li>点击"打印"将文档发送至所选打印机</li>
<li>验证文档是否按所选设置打印</li>
</ol>
</div>
## C# 中的打印对话框如何工作?
[`Printer.ShowPrintDialog()`](https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html)方法打开操作系统的本机打印对话框。 用户可以看到完整的打印选项:打印机选择、拷贝数量、页面范围、方向和纸张大小。他们可以点击"打印"发送作业,或者点击"取消"来关闭对话框而不打印。
在背后,本机.NET方法需要创建一个`System.Windows.Forms.PrintDialog`实例,将其连接到<a href="https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument">`PrintDocument.Print()`。 该配置通常需要 15 至 25 行代码。 其本身也不包括内置PDF或图像渲染(通过本地对话框打印PDF意味着首先需要解析PDF为可绘制页面,这需要另一个库)。</a>
IronPrint 通过一次调用即可处理整个流程:
### 输入
传递给`ShowPrintDialog`的quarterly-report.pdf,一个设计好的公司季度财务摘要,包含KPI指标卡和部门收入表,代表一种典型的业务文档被发送到打印对话框。
<iframe loading="lazy" src="/static-assets/print/how-to/print-with-dialog/quarterly-report.pdf" width="100%" height="400px"></iframe>
```cs
:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-show-print-dialog-open-and-print.cs
```
### 输出
<center>
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/print/how-to/print-with-dialog/print-dialogue-output-setting-default.webp" target="_blank"><img src="/static-assets/print/how-to/print-with-dialog/print-dialogue-output-setting-default.webp" alt="通过ShowPrintDialog打开的本地打印对话框" class="img-responsive add-shadow" /></a>
<p class="content__image-caption" style="color: #181818; font-style: italic;">通过ShowPrintDialog为季度报告打开的本地打印对话框。</p>
</div>
</center>
该方法接受文件路径作为`byte[]`。 IronPrint 会检测文档格式,通过相应的引擎进行渲染,并显示对话框。 用户确认后,文档将按其选择的设置进行打印。 [打印文档教程](https://ironsoftware.com/csharp/print/tutorials/print-document/)将更详细地介绍完整的打印生命周期。
## 如何预配置对话框设置?
我们可以在对话框打开之前,通过创建[`PrintSettings`](https://ironsoftware.com/csharp/print/how-to/print-settings/)对象并将其作为第二个参数传递来设置默认值。 对话框打开时将预先选中这些值,用户可以直接接受,也可以覆盖任何设置。
### 输入
预加载为A4纵向设置的invoice.pdf,300 DPI:一个带有明细项、折扣、税费和电汇说明的专业账单发票,显示一个应用程序已经知道所需纸张大小和方向的实际文档。
<iframe loading="lazy" src="/static-assets/print/how-to/print-with-dialog/invoice.pdf" width="100%" height="400px"></iframe>
```cs
:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-preconfigure-dialog-settings.cs
```
### 输出
<center>
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/print/how-to/print-with-dialog/print-dialogue-output-setting.webp" target="_blank"><img src="/static-assets/print/how-to/print-with-dialog/print-dialogue-output-setting.webp" alt="带有预配置设置的Windows打印对话框" class="img-responsive add-shadow" /></a>
<p class="content__image-caption" style="color: #181818; font-style: italic;">以预配置A4纵向设置打开的本地打印对话框。</p>
</div>
</center>
当应用程序能预先知晓可能使用的打印机或纸张规格时,此功能非常有用。 例如,一个始终在特定的热敏打印机上打印收据的销售点系统可以将`PrinterName`默认为该打印设备。用户仍然可以在对话框中更改它。
要发现系统上有哪些打印机可用,我们调用<a href="https://ironsoftware.com/csharp/print/examples/print-settings/">`List<string>`,其中包含所有已安装的打印机。 类似地,`Printer.GetPrinterTrays()`返回给定打印机的可用纸盘。</a>
可配置属性的完整列表包括`Tray`。 [打印设置](https://ironsoftware.com/csharp/print/how-to/print-settings/)指南通过代码示例详细介绍了每个属性。 在`PrintSettings`中未设置的任何属性默认为选定打印机的标准配置。
## 如何异步显示对话框?
<a href="https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html">`await`兼容。 这可以防止对话框阻塞UI线程,这对于WPF、MAUI和任何冻结界面会带来不良用户体验的应用程序至关重要。</a>
### 输入
report.pdf异步等待:一个多部分的IT基础设施和安全报告,代表一种长篇文档,在文件加载时,非阻塞对话框展示保持界面响应。
<iframe loading="lazy" src="/static-assets/print/how-to/print-with-dialog/report.pdf" width="100%" height="400px"></iframe>
```cs
:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-show-print-dialog-async.cs
```
### 输出
<center>
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/print/how-to/print-with-dialog/async-print-dialogue.webp" target="_blank"><img src="/static-assets/print/how-to/print-with-dialog/async-print-dialogue.webp" alt="通过ShowPrintDialogAsync打开的异步打印对话框" class="img-responsive add-shadow" /></a>
<p class="content__image-caption" style="color: #181818; font-style: italic;">通过ShowPrintDialogAsync异步打开的本地打印对话框,控制台确认非阻塞执行。</p>
</div>
</center>
`PrintSettings`对象。 异步模式遵循现代 .NET 开发中通用的[基于 Task 的异步模式](https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap)。
IronPrint 支持 WinForms、WPF、MAUI 及控制台应用程序。 对话框的外观会根据宿主平台和操作系统版本进行适配,因此用户始终能看到其预期的原生打印窗口。
## 何时应使用对话框打印,何时应使用静默打印?
当用户需要在每个作业前选择打印机设置时使用对话框; 在不需要人为决策的自动化工作流程中使用静默打印。
|翻译标准| 带对话框的打印 |无声印刷|
|---|---|---|
| 用户交互 | 用户选择打印机、复印、页码范围 | 无需交互; prints immediately |
| 最适合 | 桌面应用、单次打印、面向用户的功能 | 批处理作业、后台服务、自助终端应用 |
|打印机选择| 用户通过对话框进行选择 |通过`PrintSettings`以编程方式设置|
| IronPrint 方法 | `Printer.ShowPrintDialog()` | `Printer.Print()` |
| 异步版本 | `ShowPrintDialogAsync()` | `PrintAsync()` |
报告导出、发票打印以及任何错误打印机会浪费材料的作业都是对话框的理想人选。 [静默打印](https://ironsoftware.com/csharp/print/how-to/silent-printing/)适合不需要人为决策的自动化工作流程。
## 打印对话框支持哪些文件格式?
`Printer.ShowPrintDialog()`支持与静默打印相同的格式:PDF、PNG、TIFF、JPEG、GIF、HTML和BMP。我们传递文件路径,无论格式如何,IronPrint处理渲染和打印后台通信。 文件数据作为`byte[]`也被接受,这在文件是在内存中生成或从数据库中检索时非常有用。
### 输入
monthly-report.pdf读取到一个`byte[]`中,代表从数据库中检索或在发送到对话框之前在内存中生成的文档。
<iframe loading="lazy" src="/static-assets/print/how-to/print-with-dialog/monthly-report.pdf" width="100%" height="400px"></iframe>
```cs
:path=/static-assets/print/content-code-examples/how-to/print-with-dialog/print-with-dialog-print-dialog-image-and-byte-array.cs
```
### 输出
对话框像上面的例子中一样打开。 格式检测是自动的:无论我们传递的是文件路径还是`byte[]`,IronPrint都会渲染文档并呈现相同的本机对话框。
[代码](https://ironsoftware.com/csharp/print/examples/print-with-dialog/)示例页面展示了更多特定于不同格式的应用场景。 对于特定于PDF的工作流程(生成PDF并立即打印),[IronPDF](https://ironpdf.com)与IronPrint自然而然地配合使用。
## 下一步
通过对话框进行打印归结为两种方法:用于同步调用的`Printer.ShowPrintDialogAsync()`。 使用`PrintSettings`预先配置默认值,并让用户从那里进行调整。 这两种方法均支持 IronPrint 的所有[文档格式](https://ironsoftware.com/csharp/print/),并适用于 WinForms、WPF、MAUI 和控制台项目。
请查阅 [IronPrint 教程](https://ironsoftware.com/csharp/print/tutorials/print-document/)以获取完整操作指南,查阅 [Printer 类 API 参考](https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html)以了解所有可用方法,或[查阅打印设置指南](https://ironsoftware.com/csharp/print/how-to/print-settings/)以进行高级配置。 更新日志记录了最近的改进和新功能。
[开始免费30天试用](https://ironsoftware.com/csharp/print/#trial-license)在实时项目中测试对话框打印,无需信用卡。 在准备好发布时,[查看许可证选项](https://ironsoftware.com/csharp/print/licensing/),起价为`$liteLicense`。
Ask ChatGPT about this page
Ask Gemini about this page
Ask Perplexity about this page
IronPrint 提供Printer.ShowPrintDialog():一种方法,用于打开本机操作系统打印对话框,捕获用户的打印机和纸张选择,并将文档发送到打印队列。 安装一个NuGet包并写一行代码。
打印对话框是标准的操作系统窗口,允许用户在打印前选择打印机、设置副本、选择页面范围和调整纸张选项。 需要在每个作业前给予用户控制权的桌面应用程序是主要的使用案例。
1 Install IronPrint with NuGet Package Manager
PM > Install-Package IronPrint
Install-Package IronPrint
2 复制并运行这段代码。
using IronPrint ;
// Display the print dialog and print the document
Printer . ShowPrintDialog ( "document.pdf" );
using IronPrint;
// Display the print dialog and print the document
Printer.ShowPrintDialog("document.pdf");
C#
3 部署到您的生产环境中进行测试
通过免费试用 立即在您的项目中开始使用IronPrint
免费试用30天
最小工作流程(5 个步骤) 安装 IronPrint C# 打印库 调用 Printer.ShowPrintDialog("filepath") 用户选择打印机、副本数和页码范围 点击"打印"将文档发送至所选打印机 验证文档是否按所选设置打印
C# 中的打印对话框如何工作?
Printer.ShowPrintDialog() 方法打开操作系统的本机打印对话框。 用户可以看到完整的打印选项:打印机选择、拷贝数量、页面范围、方向和纸张大小。他们可以点击"打印"发送作业,或者点击"取消"来关闭对话框而不打印。
在背后,本机.NET方法需要创建一个System.Windows.Forms.PrintDialog实例,将其连接到PrintDocument.Print()。 该配置通常需要 15 至 25 行代码。 其本身也不包括内置PDF或图像渲染(通过本地对话框打印PDF意味着首先需要解析PDF为可绘制页面,这需要另一个库)。
IronPrint 通过一次调用即可处理整个流程:
输入
传递给ShowPrintDialog的quarterly-report.pdf,一个设计好的公司季度财务摘要,包含KPI指标卡和部门收入表,代表一种典型的业务文档被发送到打印对话框。
using IronPrint ;
// Open the print dialog and print
Printer . ShowPrintDialog ( "quarterly-report.pdf" ); using IronPrint;
// Open the print dialog and print
Printer.ShowPrintDialog("quarterly-report.pdf");
Imports IronPrint
' Open the print dialog and print
Printer . ShowPrintDialog ( "quarterly-report.pdf" ) Imports IronPrint
' Open the print dialog and print
Printer.ShowPrintDialog("quarterly-report.pdf")
输出
通过ShowPrintDialog为季度报告打开的本地打印对话框。
该方法接受文件路径作为byte[]。 IronPrint 会检测文档格式,通过相应的引擎进行渲染,并显示对话框。 用户确认后,文档将按其选择的设置进行打印。 打印文档教程 将更详细地介绍完整的打印生命周期。
如何预配置对话框设置?
我们可以在对话框打开之前,通过创建PrintSettings 对象并将其作为第二个参数传递来设置默认值。 对话框打开时将预先选中这些值,用户可以直接接受,也可以覆盖任何设置。
输入
预加载为A4纵向设置的invoice.pdf,300 DPI:一个带有明细项、折扣、税费和电汇说明的专业账单发票,显示一个应用程序已经知道所需纸张大小和方向的实际文档。
using IronPrint ;
// Pre-configure default dialog settings
var settings = new PrintSettings
{
PrinterName = "HP LaserJet Pro" ,
PaperSize = PaperSize . A4 ,
PaperOrientation = PaperOrientation . Portrait ,
Dpi = 300 ,
NumberOfCopies = 2 ,
Grayscale = false
};
// Open the dialog with pre-filled settings
Printer . ShowPrintDialog ( "invoice.pdf" , settings); using IronPrint;
// Pre-configure default dialog settings
var settings = new PrintSettings
{
PrinterName = "HP LaserJet Pro",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false
};
// Open the dialog with pre-filled settings
Printer.ShowPrintDialog("invoice.pdf", settings);
Imports IronPrint
' Pre-configure default dialog settings
Dim settings As New PrintSettings With {
. PrinterName = "HP LaserJet Pro" ,
. PaperSize = PaperSize . A4 ,
. PaperOrientation = PaperOrientation . Portrait ,
. Dpi = 300 ,
. NumberOfCopies = 2 ,
. Grayscale = False
}
' Open the dialog with pre-filled settings
Printer . ShowPrintDialog ( "invoice.pdf" , settings) Imports IronPrint
' Pre-configure default dialog settings
Dim settings As New PrintSettings With {
.PrinterName = "HP LaserJet Pro",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False
}
' Open the dialog with pre-filled settings
Printer.ShowPrintDialog("invoice.pdf", settings)
输出
以预配置A4纵向设置打开的本地打印对话框。
当应用程序能预先知晓可能使用的打印机或纸张规格时,此功能非常有用。 例如,一个始终在特定的热敏打印机上打印收据的销售点系统可以将PrinterName默认为该打印设备。用户仍然可以在对话框中更改它。
要发现系统上有哪些打印机可用,我们调用List<string>,其中包含所有已安装的打印机。 类似地,Printer.GetPrinterTrays()返回给定打印机的可用纸盘。
可配置属性的完整列表包括Tray。 打印设置 指南通过代码示例详细介绍了每个属性。 在PrintSettings中未设置的任何属性默认为选定打印机的标准配置。
如何异步显示对话框?
await兼容。 这可以防止对话框阻塞UI线程,这对于WPF、MAUI和任何冻结界面会带来不良用户体验的应用程序至关重要。
输入
report.pdf异步等待:一个多部分的IT基础设施和安全报告,代表一种长篇文档,在文件加载时,非阻塞对话框展示保持界面响应。
using IronPrint ;
// Open the print dialog asynchronously
await Printer . ShowPrintDialogAsync ( "report.pdf" ); using IronPrint;
// Open the print dialog asynchronously
await Printer.ShowPrintDialogAsync("report.pdf");
Imports IronPrint
' Open the print dialog asynchronously
Await Printer . ShowPrintDialogAsync ( "report.pdf" ) Imports IronPrint
' Open the print dialog asynchronously
Await Printer.ShowPrintDialogAsync("report.pdf")
输出
通过ShowPrintDialogAsync异步打开的本地打印对话框,控制台确认非阻塞执行。
PrintSettings对象。 异步模式遵循现代 .NET 开发中通用的基于 Task 的异步模式 。
IronPrint 支持 WinForms、WPF、MAUI 及控制台应用程序。 对话框的外观会根据宿主平台和操作系统版本进行适配,因此用户始终能看到其预期的原生打印窗口。
何时应使用对话框打印,何时应使用静默打印?
当用户需要在每个作业前选择打印机设置时使用对话框; 在不需要人为决策的自动化工作流程中使用静默打印。
报告导出、发票打印以及任何错误打印机会浪费材料的作业都是对话框的理想人选。 静默打印 适合不需要人为决策的自动化工作流程。
打印对话框支持哪些文件格式?
Printer.ShowPrintDialog()支持与静默打印相同的格式:PDF、PNG、TIFF、JPEG、GIF、HTML和BMP。我们传递文件路径,无论格式如何,IronPrint处理渲染和打印后台通信。 文件数据作为byte[]也被接受,这在文件是在内存中生成或从数据库中检索时非常有用。
输入
monthly-report.pdf读取到一个byte[]中,代表从数据库中检索或在发送到对话框之前在内存中生成的文档。
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); 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) 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)
输出
对话框像上面的例子中一样打开。 格式检测是自动的:无论我们传递的是文件路径还是byte[],IronPrint都会渲染文档并呈现相同的本机对话框。
代码 示例页面展示了更多特定于不同格式的应用场景。 对于特定于PDF的工作流程(生成PDF并立即打印),IronPDF 与IronPrint自然而然地配合使用。
下一步
通过对话框进行打印归结为两种方法:用于同步调用的Printer.ShowPrintDialogAsync()。 使用PrintSettings预先配置默认值,并让用户从那里进行调整。 这两种方法均支持 IronPrint 的所有文档格式 ,并适用于 WinForms、WPF、MAUI 和控制台项目。
请查阅 IronPrint 教程 以获取完整操作指南,查阅 Printer 类 API 参考 以了解所有可用方法,或查阅打印设置指南 以进行高级配置。 更新日志记录了最近的改进和新功能。
开始免费30天试用 在实时项目中测试对话框打印,无需信用卡。 在准备好发布时,查看许可证选项 ,起价为$999。
常见问题解答 IronPrint 允许开发人员在 C# 应用程序中显示和配置打印对话框,轻松打印具有预配置设置的 PDF 和图像。
您可以通过使用 IronPrint 的 ShowPrintDialog() 方法在您的 C# 应用程序中显示打印对话框,它提供了一种交互配置打印设置的方式。
是的,IronPrint 支持异步打印,这允许您的应用程序在处理打印任务时保持响应。
是的,IronPrint 允许您在显示打印对话框之前预配置打印设置,例如纸张尺寸、方向和打印机选择。
IronPrint 支持打印 PDF 和图像,为您的 C# 应用程序中不同的文档类型提供了灵活性。
IronPrint 通过允许开发人员轻松自定义和配置打印设置,同时支持同步和异步操作,提升了打印对话框的体验。
using IronPrint,您可以直接从您的 C# 应用程序中打印各种文档,包括 PDF 和图像。
是的,IronPrint 设计易于集成到现有的 C# 应用程序中,提供了一种简单的方法来添加打印对话框功能。
IronPrint 提供了如易于集成、可定制的打印设置、支持异步打印以及打印 PDF 和图像的能力等优势。
IronPrint 允许您配置不同的打印机设置,确保与各种打印机的兼容性,并能根据特定打印需求进行自定义。
技术作家
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
...
阅读更多