如何在 C# 中使用對話方塊進行列印
IronPrint 提供 Printer.ShowPrintDialog():一種方法,可打開原生作業系統的列印對話框,捕捉使用者的印表機和紙張選擇,並將文件發送到列印佇列。 安裝一個 NuGet 套件並編寫一行代碼。
列印對話框是標準作業系統窗口,讓用戶選擇印表機,設置副本數,選擇頁面範圍,並在列印前調整紙張選項。 桌面應用程式需要在每個任務之前給用戶此控制能力是主要的使用案例。
快速入門:透過對話方塊進行列印-
1使用 IronPrint 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronPrint; // Display the print dialog and print the document Printer.ShowPrintDialog("document.pdf");Imports IronPrint ' Display the print dialog and print the document Printer.ShowPrintDialog("document.pdf")Imports IronPrint ' Display the print dialog and print the document Printer.ShowPrintDialog("document.pdf") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPrint,並使用 免費試用
最小工作流程(5 個步驟)
- 安裝IronPrint C#列印程式庫
- 呼叫
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指標卡和部門收入表格格式的企業第三季度財務總結,代表通常送至列印對話框的商業文件。
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")輸出
該方法接受檔案路徑作為 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)輸出
當應用程式能預先得知可能使用的印表機或紙張格式時,此功能便十分實用。 例如,始終在特定熱敏印表機上列印收據的POS系統可以將PrinterName預設為該設備。使用者仍然可以在對話框中更改它。
要了解系統上有哪些可用的印表機,我們調用 Printer.GetPrinterNames(),它會返回所有已安裝印表機的 List<string>。 同樣,Printer.GetPrinterTrays()返回給定印表機的可用紙盤。
可配置屬性的完整列表包括PrinterName、PaperSize、PaperOrientation、Dpi、NumberOfCopies、Grayscale、PaperMargins、Flatten(用於PDF表單欄位)和Tray。 列印設置操作指南涵蓋了每個屬性並附有代碼範例。 在PrintSettings中未設置的任何屬性將默認為所選印表機的標準配置。
如何以非同步方式顯示對話方塊?
Printer.ShowPrintDialogAsync()方法返回一個Task,使其與async/await兼容。 這可防止對話框阻塞 UI 線程,這對於 WPF、MAUI 和任何介面卡住會造成不良用戶體驗的應用程式來說至關重要。
輸入
report.pdf 非同步等待:一個多段 IT 基礎設施和安全報告,代表一種長篇文件,非阻塞的對話框呈現使得在文件加載時保持 UI 響應。
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 及主控台應用程式。 對話方塊的外觀會根據主機平台和作業系統版本進行調整,因此使用者始終會看到其預期的原生列印視窗。
何時該使用對話式列印,何時該使用靜默列印?
當用戶在每個任務之前需要選擇印表機設置時,使用對話框; 對於不需要人為決定的自動化工作流程,使用靜默列印。
| 標準 | 透過對話方塊列印 | 無聲印刷 |
|---|---|---|
| 使用者互動 | 使用者選擇印表機、份數、頁碼範圍 | 無交互; 立即列印 |
| 最適合 | 桌面應用程式、單次列印、使用者端功能 | 批次工作、背景服務、資訊站應用程式 |
| 印表機選擇 | 使用者從對話方塊中選擇 | 通過PrintSettings以程式方式設置 |
| IronPrint 方法 | Printer.ShowPrintDialog() | Printer.Print() |
| 非同步版本 | ShowPrintDialogAsync() | PrintAsync() |
報告匯出、發票列印和任何選錯印表機會浪費材料的工作都是對話框的好候選者。 靜默列印適合自動化工作流程,文件之間不需要人工決策。
「列印對話方塊」支援哪些檔案格式?
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 的列印對話框功能是什麼?
IronPrint 的列印對話框功能使用 'Printer.ShowPrintDialog()' 方法,這會啟動本機 OS 列印對話框,讓使用者選擇列印機設置並將文件發送到列印佇列。
IronPrint 如何簡化 C# 的列印過程?
IronPrint 簡化了列印過程,允許使用者調用一個方法('Printer.ShowPrintDialog')來打開 OS 本機列印對話框,並處理文件發送,所需代碼遠少於本機 .NET Framework。
我可以使用 IronPrint 預先配置列印對話框設置嗎?
可以,您可以在 IronPrint 中創建一個 'PrintSettings' 對象來預先配置列印對話框設置,這允許您在打開對話框之前設置預設列印機屬性。
如何使用 IronPrint 異步顯示列印對話框?
您可以通過使用 'Printer.ShowPrintDialogAsync()' 方法與 IronPrint 異步顯示列印對話框,這與 async/await 模式相兼容,並防止 UI 阻塞。
什麼時候應該使用列印對話框而不是使用 IronPrint 的靜默列印?
當需要使用者手動選擇列印設置時,應使用列印對話框,而靜默列印則適合於不需要使用者互動的自動化工作流程。
IronPrint 的列印對話框支援哪些文件格式?
IronPrint 的列印對話框支援多種格式,包括 PDF、PNG、TIFF、JPEG、GIF、HTML 和 BMP。文件處理可以通過檔案路徑或位元組數組來完成。
IronPrint 如何處理不同的文件格式?
IronPrint 會在提供檔案路徑或位元組數組時自動檢測文件格式,並管理渲染和列印相關程序之間的通訊,以支援的格式進行處理。
IronPrint 的對話框支持哪些平台?
IronPrint 的列印對話框與 WinForms、WPF、MAUI 和控制台應用程序兼容,會根據主機平台和 OS 版本調整其外觀。
如何入手 IronPrint 的列印對話框功能?
要入門,請通過 NuGet 安裝 IronPrint 程式庫,然後使用 'Printer.ShowPrintDialog()' 編寫一行簡單的代碼,並可選擇性地使用 'PrintSettings' 對象進行設置配置。
IronPrint 可以用於列印動態生成的內容嗎?
可以,IronPrint 能夠處理以位元組數組形式接受的動態生成或從數據庫檢索的文檔列印,這在動態創建或從數據庫檢索的文檔時非常實用。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。


