IronPrint 操作指南 指定印表機名稱 如何在 C# 中指定印表機名稱 Curtis Chau 更新:2026年3月2日 下載 IronPrint NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronPrint 的 PrintSettings 類別提供了一個 PrinterName 屬性,用於將列印工作導向至特定印表機。 我們將目標印表機的正確名稱作為字串傳入,並將已設定的 PrintSettings 物件傳遞給 IronPrint 的任何列印方法,如此一來,文件便會傳送至該印表機,而非系統預設的印表機。 本指南將逐步說明如何設定印表機名稱、在執行階段偵測可用印表機,以及將印表機選擇與其他列印設定結合使用。 快速入門:指定印表機名稱 透過 NuGet 安裝 IronPrint:Install-Package IronPrint 將 using IronPrint; 加入檔案中 建立一個 PrintSettings 物件 將 PrinterName 設定為目標印表機的確切名稱 將設定傳遞至 Printer.Print() 或 Printer.PrintAsync() 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPrint PM > Install-Package IronPrint 複製並運行這段程式碼。 using IronPrint; // Print a document to a specific printer Printer.Print("report.pdf", new PrintSettings { PrinterName = "HP LaserJet Pro M404" }); 部署到您的生產環境進行測試 今天就在您的專案中開始使用免費試用IronPrint Free 30 Day Trial 最小工作流程(5 個步驟) 安裝 IronPrint C# 程式庫 建立一個 `PrintSettings` 物件 將 `PrinterName` 設定為目標印表機的名稱 將設定傳遞給 `Printer.Print()` 執行專案以在指定印表機上列印 如何在 C# 中指定印表機名稱? 我們透過將印表機名稱指派給 PrintSettings 物件的 PrinterName 屬性,來指定目標印表機。 接著,我們將該物件傳遞給 Printer.Print。 :path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-set-printer-name.cs using IronPrint; // Configure print settings with a target printer PrintSettings settings = new PrintSettings(); settings.PrinterName = "Microsoft Print to PDF"; // Send the document to the specified printer Printer.Print("invoice.pdf", settings); Imports IronPrint ' Configure print settings with a target printer Dim settings As New PrintSettings() settings.PrinterName = "Microsoft Print to PDF" ' Send the document to the specified printer Printer.Print("invoice.pdf", settings) $vbLabelText $csharpLabel 我們首先實例化 PrintSettings,其初始化參數為 PrinterName = null —— 即作業系統的預設印表機。 接著,我們將 PrinterName 替換為目標印表機的精確字串名稱。 當我們呼叫 Printer.Print 時,IronPrint 會將該列印工作直接傳送至該印表機的列印佇列。 有兩項關鍵細節需特別留意。首先,印表機名稱必須與作業系統回報的名稱完全一致——此比對區分大小寫。若名稱不符(例如 "hp laserjet" 而非 "HP LaserJet"),系統將靜默失敗或拋出錯誤。 其次,若使用者透過 ShowPrintDialog 開啟列印對話方塊,該對話方塊的選項將覆寫程式碼中設定的 PrinterName 值。 這是刻意設計的 — 對話方塊賦予使用者最終控制權。 如何偵測可用的印表機? 與其硬編碼印表機名稱,我們可以在執行時透過 Printer.GetPrinterNames() 查詢系統。 此方法會傳回一個 List<string>,其中包含該機器上所有已安裝的印表機。 :path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-discover-printers.cs using IronPrint; // Discover all installed printers List<string> printers = Printer.GetPrinterNames(); foreach (string name in printers) { Console.WriteLine(name); } // Use the first available printer if (printers.Count > 0) { Printer.Print("report.pdf", new PrintSettings { PrinterName = printers[0] }); } Imports IronPrint ' Discover all installed printers Dim printers As List(Of String) = Printer.GetPrinterNames() For Each name As String In printers Console.WriteLine(name) Next ' Use the first available printer If printers.Count > 0 Then Printer.Print("report.pdf", New PrintSettings With { .PrinterName = printers(0) }) End If $vbLabelText $csharpLabel 我們呼叫 GetPrinterNames() 來擷取作業系統所識別的所有印表機 — 包括本地、網路及"Microsoft Print to PDF"這類虛擬印表機。接著,我們會遍歷該清單,並透過索引、名稱比對或應用程式所需的任何自訂邏輯來選取印表機。 這種"先偵測再列印"的模式對於部署在不同機器上的應用程式至關重要。 在單一電腦環境中,硬編碼印表機名稱是可行的,但生產環境中的應用程式應查詢可用的印表機,並讓使用者自行選擇,或根據命名規範透過程式碼自動選取。 如需專用的程式碼範例,請參閱"取得印表機名稱"範例。 如何將印表機名稱與其他設定結合? PrintSettings 類別提供了 PrinterName 以及紙張尺寸、方向、DPI、邊距、複印份數和扁平化等屬性。 我們將所有設定整合於單一物件中。 :path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-combined-settings.cs using IronPrint; // Build a fully configured print job targeting a specific 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 a branded report to the color printer Printer.Print("quarterly-report.pdf", settings); Imports IronPrint ' Build a fully configured print job targeting a specific printer Dim settings As New PrintSettings With { .PrinterName = "Office Color Printer", .PaperSize = PaperSize.A4, .PaperOrientation = PaperOrientation.Portrait, .Dpi = 300, .NumberOfCopies = 2, .PaperMargins = New Margins(15, 15, 15, 15), .Grayscale = False } ' Print a branded report to the color printer Printer.Print("quarterly-report.pdf", settings) $vbLabelText $csharpLabel 我們採用物件初始化語法以提升可讀性。 PrinterName 將工作排程至"Office 彩色印表機",其餘屬性則用於控制輸出格式。 Dpi 設定為 300 時,可呈現清晰的文字與圖形。 PaperMargins 透過 Margins 建構函式接受四個毫米單位的數值 — 頂部、右側、底部、左側。 IronPrint 會將設定作為一個整體進行驗證,並將整合後的設定以單一列印工作提交給印表機驅動程式。 如需了解托盤選擇和灰階模式等其他選項,請參閱完整的列印設定指南。 如何選擇印表機並進行非同步列印? 對於無法阻塞主執行緒的應用情境(例如 WPF 或 WinForms 應用程式),我們會使用 Printer.GetPrinterNamesAsync() 和 Printer.PrintAsync()。 兩者皆會傳回 Task,以維持使用者介面的反應速度。 :path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-async-printer-select.cs using IronPrint; using System.Threading.Tasks; public class PrintService { public async Task PrintToFirstAvailableAsync(string filePath) { // Discover printers without blocking the UI List<string> printers = await Printer.GetPrinterNamesAsync(); if (printers.Count == 0) { Console.WriteLine("No printers found."); return; } // Configure and print to the first available printer PrintSettings settings = new PrintSettings { PrinterName = printers[0], Dpi = 300 }; await Printer.PrintAsync(filePath, settings); } } Imports IronPrint Imports System.Threading.Tasks Public Class PrintService Public Async Function PrintToFirstAvailableAsync(filePath As String) As Task ' Discover printers without blocking the UI Dim printers As List(Of String) = Await Printer.GetPrinterNamesAsync() If printers.Count = 0 Then Console.WriteLine("No printers found.") Return End If ' Configure and print to the first available printer Dim settings As New PrintSettings With { .PrinterName = printers(0), .Dpi = 300 } Await Printer.PrintAsync(filePath, settings) End Function End Class $vbLabelText $csharpLabel 此基於類別的範例將"偵測並列印"的邏輯封裝成可重複使用的服務。我們呼叫 GetPrinterNamesAsync() 來擷取印表機清單,同時避免凍結使用者介面,接著將第一個可用的印表機指派給 PrinterName。 await Printer.PrintAsync 呼叫會以非同步方式傳送工作。 在實際部署時,我們可能會將 printers[0] 替換為符合命名規範的邏輯——例如搜尋名稱中包含"Label"的印表機以列印運送標籤,或搜尋名稱中包含"Color"的印表機以列印品牌文件。 IronPrint 的所有非同步方法皆接受相同的 PrintSettings 物件,因此 PrinterName 在同步與非同步路徑間的行為完全一致。 接下來我該怎麼做? 我們已說明如何在 C# 中使用 IronPrint 的 PrintSettings.PrinterName 屬性指定印表機名稱,內容涵蓋從靜態指派到透過 Printer.GetPrinterNames() 進行動態執行時偵測。 重點說明:印表機名稱必須完全一致(區分大小寫),null 預設使用作業系統的預設印表機,且列印對話方塊會覆寫程式化的選取設定。 若要進一步探索 IronPrint 的功能: 請參閱"取得印表機名稱"的程式碼範例,以了解獨立運作的偵測程式碼片段 探索完整的列印設定配置指南,了解所有可用屬性 請參閱 Printer 類別的 API 參考文件,了解 ShowPrintDialog 等方法以及紙匣管理功能 瀏覽 PaperSize 及 Margins 的完整 API 參考文件 立即開始 30 天試用,在您的專案中測試印表機選項,或查看生產環境部署的授權方案。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 38,930 | 版本: 2026.4 剛剛發布 開始免費試用 免費 NuGet 下載 總下載量:38,930 查看許可證 還在捲動嗎? 想要快速證明? PM > Install-Package IronPrint 執行範例 觀看您的文件打到印表機上。 免費 NuGet 下載 總下載量:38,930 查看許可證