使用 IRONPRINT C# 將 PDF 發送到打印機(逐步教程) Curtis Chau 更新:6月 22, 2025 下載 IronPrint NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 IronPrint是 Iron Software 全新推出的 .NET 列印庫,相容於包括 Windows、macOS、Android 和 iOS 在內的多種平台。立即開始使用 IronPrint ! PDF 指的是"便攜式文件格式"。 在很多情況下,開發人員需要在應用程式中以程式方式列印 PDF 檔案。 在 C# 中,這可能是一項非常繁瑣的任務,但多虧了 IronPDF,只需幾行程式碼就能輕鬆完成。 該工具允許我們使用預設印表機設定以及自訂列印選項來列印 PDF 文件。 在本教程中,您將學習如何使用 C# 語言列印 PDF 檔案。 本教程涵蓋的主題 本文將涵蓋以下主題: IronPDF 庫 建立 C# 控制台項目 安裝 IronPDF NuGet 套件管理器 NuGet 套件管理器控制台 使用 DLL 文件 新增 IronPDF 命名空間 列印 PDF 文件 建立 PDF 文件並列印 PDF 文件 根據 URL 建立 PDF 文件並列印 先進印刷技術 概括 如何在 C# 中將 PDF 傳送到印表機 安裝 C# 庫以將 PDF 傳送到印表機 使用預設印表機設定和Print方式列印 PDF 文件 透過設定PrinterName屬性將郵件傳送到指定的印表機 設定PrinterResolution屬性可自訂印表機解析度 在 C# 中保持對打印頁數的跟踪 IronPDF。 IronPDF 是一個適用於 .NET Framework 的 PDF 程式庫,它允許開發人員輕鬆建立 PDF 檔案。 IronPDF 在 Google Chrome 桌面版上的渲染效果"像素完美"。 IronPDF 只需一行程式碼即可輕鬆建立 PDF 文件。 它無需 Acrobat Reader 或其他 PDF 檢視器即可處理 PDF 文件。 IronPDF 可用於從 HTML 字串、HTML 檔案或 URL 建立 PDF 檔案。 之後,它可以將這些文件發送到預設印表機進行列印。 A free trial of IronPDF is available. IronPDF。庫的一些重要特性 使用 HTML 4 和 5、CSS 和 JavaScript 建立 PDF 文檔 根據 URL 產生 PDF 文檔 將 PDF 檔案列印到預設的實體印表機 設定列印作業設定(例如,列印特定頁面等) 載入包含自訂網頁登入憑證、使用者代理程式、代理伺服器、Cookie、HTTP 標頭和表單欄位或變數的 URL,從而允許存取 HTML 登入表單後面的網頁。 讀取並填寫 PDF(便攜式文件格式)表單欄位數據 從 PDF 檔案中提取圖像和文字 對 PDF 文件進行數位簽名 無需第三方函式庫 1. 建立一個 C# 項目 本教學將使用 Visual Studio 2022,但您也可以使用更早的版本。 開啟 Visual Studio 2022。 建立一個新的 C# .NET 控制台專案。 選擇 .NET Core 控制台應用程式。 控制台應用程式 為項目命名,例如 DemoApp。 我們將使用 .NET Framework 6.0 的最新、最穩定的版本。 點擊"創建"按鈕。 .NET Framework 2. 安裝 IronPDF 庫 要安裝 IronPDF 庫,我們可以使用以下任何一種方法: 2.1. NuGet 套件管理器 我們可以從 NuGet 套件管理器安裝 IronPDF C# .NET Core 庫。 按一下"工具">"NuGet 套件管理員">"管理解決方案的 NuGet 套件",開啟套件管理員。 軟體套件管理器 或者,在解決方案資源管理器中以滑鼠右鍵按一下項目,然後按一下"管理 NuGet 套件"。 NuGet 套件管理器 - 解決方案資源管理器 搜尋 IronPDF。 選擇 IronPDF 並點擊安裝。 庫將開始安裝。 安裝 IronPDF 2.2. NuGet 套件管理器控制台 按一下"工具">"NuGet 套件管理器">"套件管理器控制台",開啟 NuGet 套件管理員控制台。 在命令列中輸入以下命令: Install-Package IronPrint 套件管理控制台 2.3. 使用 DLL 文件 在專案中使用 IronPDF 的另一種方法是新增 IronPDF 庫中的 DLL 檔案。 你可以從這個鏈接下載DLL檔。 下載 DLL 壓縮檔。將其解壓縮到指定資料夾。 在 Visual Studio 中開啟一個專案。 在解決方案資源管理器中,以滑鼠右鍵按一下"引用",然後瀏覽並找到 IronPDF DLL 檔案。 2.4. 新增 IronPDF 命名空間 安裝完成後,將 IronPDF 和System.Drawing命名空間加入您的程式檔案。 using IronPdf; using System.Drawing; using IronPdf; using System.Drawing; Imports IronPdf Imports System.Drawing $vbLabelText $csharpLabel 注意:您必須將這些引用新增至您希望使用 IronPDF 功能的每個文件中。 IronPDF 已安裝完畢,可以使用了! 現在我們可以為 .NET Core 應用程式建立第一個 PDF 文檔,並將其傳送到預設印表機進行列印。 下面我們透過程式碼範例來看一下其中的一些範例。 3. 列印PDF文檔 3.1. 從 HTML 建立並列印 PDF 文檔 處理 HTML 字串並將其轉換為 PDF 格式非常容易。 然後可以使用 IronPDF 列印這個新建立的文件。 以下程式碼可以輕鬆建立 PDF 檔案。 // Create an instance of ChromePdfRenderer var chromePdfRenderer = new IronPdf.ChromePdfRenderer(); // Render any HTML fragment to a PDF document using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>"); // Send the PDF to the default printer pdfDocument.Print(); // Create a PrintDocument object that can be used for further configurations System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument(); // Create an instance of ChromePdfRenderer var chromePdfRenderer = new IronPdf.ChromePdfRenderer(); // Render any HTML fragment to a PDF document using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>"); // Send the PDF to the default printer pdfDocument.Print(); // Create a PrintDocument object that can be used for further configurations System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument(); ' Create an instance of ChromePdfRenderer Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer() ' Render any HTML fragment to a PDF document Dim pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>") ' Send the PDF to the default printer pdfDocument.Print() ' Create a PrintDocument object that can be used for further configurations Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument() $vbLabelText $csharpLabel 這段程式碼將使用RenderHtmlAsPdf函數傳遞的 HTML 內容建立一個 PDF 檔案。 此函數將 HTML 片段轉換為 PDF 文件。 若要使用 IronPDF 庫產生 PDF 檔案或 PDF 頁面,您必須熟悉 HTML 標籤。 我們使用Print功能將 PDF 文件的輸出傳送到印表機。 印表機對話方塊將會出現,您可以確認列印作業。 3.2. 從 URL 建立並列印 PDF 文檔 您也可以使用 URL 建立 PDF 文件: // Create an instance of ChromePdfRenderer var chromePdfRenderer = new IronPdf.ChromePdfRenderer(); // Render a PDF from a URL var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/"); // Send the PDF to the default printer pdfDocument.Print(); // Create a PrintDocument object that can be used for further configurations System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument(); // Create an instance of ChromePdfRenderer var chromePdfRenderer = new IronPdf.ChromePdfRenderer(); // Render a PDF from a URL var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/"); // Send the PDF to the default printer pdfDocument.Print(); // Create a PrintDocument object that can be used for further configurations System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument(); ' Create an instance of ChromePdfRenderer Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer() ' Render a PDF from a URL Dim pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/") ' Send the PDF to the default printer pdfDocument.Print() ' Create a PrintDocument object that can be used for further configurations Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument() $vbLabelText $csharpLabel 這裡,根據指定的 URL 建立 PDF 文件,然後將其列印出來。 列印從 URL 產生的 PDF 文件 4. 進階列印選項 IronPDF 功能全面,完全能夠處理列印功能,例如尋找印表機和設定列印解析度。 4.1 指定印表機 若要指定印表機,您只需取得目前列印文件物件(使用GetPrintDocument方法),然後使用PrinterSettings.PrinterName屬性即可。 您可以選擇任何可用的印表機。 using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Print the document printDocument.Print(); } using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Print the document printDocument.Print(); } Using printDocument = pdfDocument.GetPrintDocument() ' Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF" ' Print the document printDocument.Print() End Using $vbLabelText $csharpLabel 在上面的程式碼範例中,選擇了"Microsoft Print to PDF"作為印表機。 有關設定特定列印設定的更多信息,請參閱文件頁面。 4.2 設定印表機分辨率 您也可以設定 PDF 列印解析度。 解析度是指以像素數量衡量的列印品質。 您可以使用 PDF 文件的DefaultPageSettings.PrinterResolution屬性來設定列印文件的解析度。 // Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution { Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution { Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; ' Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With { .Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, .X = 1200, .Y = 1200 } $vbLabelText $csharpLabel 4.3 使用 C# 追蹤列印過程 在以下程式碼範例中,您將看到如何變更印表機名稱、設定解析度以及取得已列印頁數。 int printedPages; using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution { Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Track number of printed pages printedPages = 0; printDocument.PrintPage += (sender, args) => printedPages++; // Print the document printDocument.Print(); } int printedPages; using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution { Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Track number of printed pages printedPages = 0; printDocument.PrintPage += (sender, args) => printedPages++; // Print the document printDocument.Print(); } Dim printedPages As Integer Using printDocument = pdfDocument.GetPrintDocument() ' Specify the printer name printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF" ' Set custom printer resolution printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With { .Kind = System.Drawing.Printing.PrinterResolutionKind.Custom, .X = 1200, .Y = 1200 } ' Track number of printed pages printedPages = 0 'INSTANT VB WARNING: An assignment within expression was extracted from the following statement: 'ORIGINAL LINE: printDocument.PrintPage += (sender, args) => printedPages++; AddHandler printDocument.PrintPage, Sub(sender, args) printedPages printedPages += 1 ' Print the document printDocument.Print() End Using $vbLabelText $csharpLabel 5. 總結 IronPDF 是一個用於處理 PDF 文件的完整解決方案。 它能夠將不同格式的文件轉換為 PDF 格式。 透過 IronPDF 庫函數,PDF 文件的操作和格式化變得非常容易。 只需幾行程式碼即可建立和格式化 PDF 文件。它還可以透過程式列印 PDF 文件。 它透過將 PDF 文件發送到電腦的預設印表機來實現這一點。 我們既可以向使用者顯示列印對話框窗口,也可以使用Print方法的重載進行靜默列印。 A free trial of IronPDF is also available to test its full potential to generate and print PDF documents in your applications. 有關許可的更多信息,請點擊此鏈接。 此外,目前的特別優惠活動讓您只需支付兩件 Iron Software 產品的價格,即可獲得五件 Iron Software 產品。 常見問題解答 如何使用 C# 程式化列印 PDF 文件? 您可以使用 IronPDF 以 C# 程式化的方式列印 PDF 文件。透過使用 Print 等方法,您可以將 PDF 傳送至預設或指定的印表機,並可自訂列印選項。 在 C# 中安裝 PDF 列印函式庫的步驟為何? 要安裝 IronPDF 函式庫,您可以使用 Visual Studio 中的 NuGet Package Manager、NuGet Package Manager Console,或透過 DLL 檔案新增函式庫。 我可以使用這個函式庫從 HTML 建立 PDF 嗎? 是的,IronPDF 允許您使用 RenderHtmlAsPdf 方法從 HTML 建立 PDF。這可以將 HTML、CSS 和 JavaScript 轉換成 PDF 文件。 如何使用這個函式庫將 PDF 傳送至特定的印表機? 要將 PDF 傳送至特定的印表機,您可以將 IronPDF 內 PrintDocument 物件的 PrinterSettings.PrinterName 屬性設定為所需的印表機名稱。 這個函式庫支援哪些進階列印選項? IronPdf 支援進階列印選項,例如指定印表機設定、調整列印解析度以及追蹤列印頁數。 是否可以使用此 PDF 函式庫設定自訂列印解析度? 是的,您可以使用 IronPDF 中 PrintDocument 物件的 DefaultPageSettings.PrinterResolution 屬性來設定自訂列印解析度。 如何使用此程式庫將 URL 呈現為 PDF? 您可以使用 IronPDF 的 RenderUrlAsPdf 方法將 URL 呈現為 PDF,此方法可讓您將網頁轉換為 PDF 文件。 此庫是否提供免費試用? 是的,IronPDF 提供免費試用版,允許使用者探索其產生和列印 PDF 文件的功能。 我可以追蹤使用這個函式庫列印的頁數嗎? 是的,IronPdf 允許您在列印工作中透過存取 PrintDocument 物件的屬性來追蹤列印的頁數。 此 PDF 列印函式庫與哪些平台相容? IronPdf 與 Windows、macOS、Android 和 iOS 等多種平台相容,因此適用於不同的開發環境。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 發表日期 12月 18, 2025 使用 IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多 發表日期 10月 19, 2025 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多 更新8月 3, 2025 C# 程式化打印 PDF(代碼示例教程) 有多種應用場景中需要用到打印為 PDF 文件的功能。 閱讀更多 C# 程式化打印 PDF(代碼示例教程)如何在 VB.NET 中打印 PDF(...
發表日期 12月 18, 2025 使用 IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多
發表日期 10月 19, 2025 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多