如何在 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>Call <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` 實例,將其連接到 [`PrintDocument`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument),處理 `PrintPage` 事件以在列印圖形表面上繪製內容,檢查 `DialogResult`,然後呼叫 `PrintDocument.Print()`。 此設置通常需要15到25行程式碼。 它還不包括內建的 PDF 或圖片渲染(通過本機對話框列印 PDF 意味著首先必須將 PDF 解析為可繪制的頁面,這需要另一個程式庫)。
IronPrint 能夠在一次呼叫中處理整個流程:
### 輸入
quarterly-report.pdf 透過 `ShowPrintDialog` 傳入,這是一份帶有 KPI 指標卡和部門收入表的樣式化企業 Q3 財務摘要,代表典型的業務文件傳送至列印對話框。
<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>
此方法接受作為 `string` 的文件路徑或作為 `byte[]` 的原始文件資料。 IronPrint 偵測文件格式,通過適當的引擎渲染並呈現對話框。 當使用者確認後,文件將按照他們選擇的設置列印。 [列印文件教學](https://ironsoftware.com/csharp/print/tutorials/print-document/)更詳細地介紹了完整的列印生命周期。
## 如何預先配置對話框設置?
我們可以通過建立 [`PrintSettings`](https://ironsoftware.com/csharp/print/how-to/print-settings/) 物件並將其作為第二個參數傳入來設置預設值。 對話框將以這些預選值打開,使用者可以按原樣接受或覆蓋任何設置。
### 輸入
invoice.pdf 預載入 A4 肖像設置,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/">`Printer.GetPrinterNames()`</a>,它返回所有已安裝印表機的 `List<string>`。 類似地,`Printer.GetPrinterTrays()` 返回給定印表機的可用紙匣。
可配置屬性的全列表包括 `PrinterName`, `PaperSize`, `PaperOrientation`, `Dpi`, `NumberOfCopies`, `Grayscale`, `PaperMargins`, `Flatten`(對於 PDF 表單欄位),和 `Tray`。 [列印設置指南](https://ironsoftware.com/csharp/print/how-to/print-settings/)涵蓋每個屬性並提供程式碼範例。 在 `PrintSettings` 中未設置的任何屬性預設為所選印表機的標準配置。
## 如何非同步顯示對話框?
[`Printer.ShowPrintDialogAsync()`](https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.Printer.html) 方法返回 `Task`,使其與 `await` 相容。 這防止了對話框阻塞 UI 執行緒,這對於 WPF、MAUI 和任何因介面凍結而導致糟糕使用者體驗的應用程式是必需的。
### 輸入
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>
`ShowPrintDialogAsync()` 接受與同步版本相同的參數:文件路徑或位元組陣列,外加可選的 `PrintSettings` 物件。 這個非同步模式遵循一致用於現代 .NET 開發的[以任務為基礎的非同步模式](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.ShowPrintDialog()` 和非阻塞執行的 `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/)以進行進階配置。 [更新記錄](https://ironsoftware.com/csharp/print/product-updates/changelog/)跟蹤最新改進和新特性。
[開始免費 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# 列印程式庫 Call Printer.ShowPrintDialog("filepath") 使用者選擇印表機、份數和頁面範圍 點擊列印將文件發送到所選的印表機 驗證文件按照所選設定列印
列印對話框在 C# 中如何運作?
Printer.ShowPrintDialog() 方法打開作業系統的本機列印對話框。 使用者可以看到完整的列印選項集:印表機選擇、份數、頁面範圍、方向和紙張大小。他們點擊列印發送工作,或取消退出對話框而不列印。
在底層,本機 .NET 方法需要建立一個 System.Windows.Forms.PrintDialog 實例,將其連接到 PrintDocument ,處理 PrintPage 事件以在列印圖形表面上繪製內容,檢查 DialogResult,然後呼叫 PrintDocument.Print()。 此設置通常需要15到25行程式碼。 它還不包括內建的 PDF 或圖片渲染(通過本機對話框列印 PDF 意味著首先必須將 PDF 解析為可繪制的頁面,這需要另一個程式庫)。
IronPrint 能夠在一次呼叫中處理整個流程:
輸入
quarterly-report.pdf 透過 ShowPrintDialog 傳入,這是一份帶有 KPI 指標卡和部門收入表的樣式化企業 Q3 財務摘要,代表典型的業務文件傳送至列印對話框。
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 為季度報告打開本機列印對話框。
此方法接受作為 string 的文件路徑或作為 byte[] 的原始文件資料。 IronPrint 偵測文件格式,通過適當的引擎渲染並呈現對話框。 當使用者確認後,文件將按照他們選擇的設置列印。 列印文件教學 更詳細地介紹了完整的列印生命周期。
如何預先配置對話框設置?
我們可以通過建立 PrintSettings 物件並將其作為第二個參數傳入來設置預設值。 對話框將以這些預選值打開,使用者可以按原樣接受或覆蓋任何設置。
輸入
invoice.pdf 預載入 A4 肖像設置,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 預設為那個裝置。使用者仍然可以選擇在對話框中更改它。
要發現系統上有哪些印表機可用,我們呼叫 Printer.GetPrinterNames() ,它返回所有已安裝印表機的 List<string>。 類似地,Printer.GetPrinterTrays() 返回給定印表機的可用紙匣。
可配置屬性的全列表包括 PrinterName, PaperSize, PaperOrientation, Dpi, NumberOfCopies, Grayscale, PaperMargins, Flatten(對於 PDF 表單欄位),和 Tray。 列印設置指南 涵蓋每個屬性並提供程式碼範例。 在 PrintSettings 中未設置的任何屬性預設為所選印表機的標準配置。
如何非同步顯示對話框?
Printer.ShowPrintDialogAsync() 方法返回 Task,使其與 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 非同步打開本機列印對話框,並透過控制台確認非阻塞執行。
ShowPrintDialogAsync() 接受與同步版本相同的參數:文件路徑或位元組陣列,外加可選的 PrintSettings 物件。 這個非同步模式遵循一致用於現代 .NET 開發的以任務為基礎的非同步模式 。
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.ShowPrintDialog() 和非阻塞執行的 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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
...
閱讀更多