使用 IRONPRINT 如何在 C# 中靜默打印 PDF 文件 Curtis Chau 更新:8月 20, 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# 開發人員來說,以程式方式列印 PDF 文件可能是一個挑戰。 透過IronPDF - C# PDF 庫,列印 PDF 文件的過程變得極為簡單便捷。 本文將介紹如何使用 IronPDF 庫在 C# 中靜默列印 PDF 文件。 如何在 C# 中靜默列印 PDF 文件 安裝 C# 庫以靜默列印 PDF 文件 使用Print方法以預設設定列印 PDF 文檔 設定PrinterName屬性以指定印表機 在 C# 中使用PrinterResolution類別設定自訂分辨率 無需開啟打印機對話框即可靜默列印 PDF 至指定目錄 IronPDF - C# PDF 庫 IronPDF 是一個 C# .NET 程式庫,它允許開發人員建立、讀取和編輯 PDF 文件。 這是一個頂級的 C# 庫,優先考慮準確性、易用性和速度。 它專為C#、F# 和 VB.NET設計,並與.NET 7、6、5、Core、Standard 或 Framework 版本高度相容。它利用 IronPDF 強大的 Chromium 引擎,幫助使用者從 HTML 產生適用於 Web、桌面和控制台的 PDF 檔案。 此外,IronPDF 還允許使用者輕鬆操作和編輯 PDF,添加頁首和頁腳,從 PDF 中提取文字和圖像。 一些重要功能包括: 從不同文件格式載入和產生 PDF 文件 使用預設印表機儲存和列印 PDF 文件 合併和拆分 PDF 文件 無需 Adobe Reader 的 PDF 編輯器 使用 IronPDF 庫在 C# 中靜默列印 PDF 文件的步驟 要靜默列印 PDF 文檔,首先需要在本機電腦上安裝以下組件。 Visual Studio - 它是 C# 開發的官方 IDE,必須安裝在電腦上。 您可以從Visual Studio 網站下載並安裝。 2.建立專案- 若要使用 Visual Studio 2022 建立一個用於 PDF 列印的 C# 控制台應用程序,請按照以下步驟操作: 開啟 Visual Studio,然後按一下"建立新專案" <div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-1.webp" alt="Visual Studio" class="img-responsive add-shadow" /> <p class="content__image-caption">Visual Studio</p> </div> 選擇 C# 控制台應用程序,然後按一下"下一步"。 <div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-2.webp" alt="新專案對話框" class="img-responsive add-shadow" /> <p class="content__image-caption">新專案對話框</p> </div> 現在,輸入您的項目名稱,選擇位置,然後按一下"下一步"。 <div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-3.webp" alt="Web Forms" class="img-responsive add-shadow" /> <p class="content__image-caption">Web Forms</p> </div> 為您的應用程式選擇最新的 .NET Framework。 我們將使用穩定版本 6.0。 附加資訊 點擊創建,控制台專案就創建好了,我們可以開始以程式方式列印 PDF 文件了。 3.安裝 IronPDF - 有 3 種方法可以下載和安裝 IronPDF 庫。 這些工具如下: -使用 Visual Studio - Visual Studio 具有 NuGet 套件管理器,可以協助在 C# 專案中安裝 NuGet 套件。 點選選單列中的"工具",或 在解決方案資源管理器中以滑鼠右鍵按一下專案文件 解決方案資源管理器 專案選單 > 管理 NuGet 套件 開啟後,在 NuGet 套件管理器中瀏覽 IronPDF 並進行安裝,如下所示: 從 NuGet 套件安裝 IronPDF -直接下載 NuGet 套件- 下載 IronPDF 的另一種方法是訪問 NuGet 網站並直接下載該包。 這是連結:https://www.nuget.org/packages/IronPdf/ 。 -下載 IronPDF .DLL 庫- 您也可以直接從 IronPDF 網站下載 IronPDF。請造訪: IronPDF DLL 下載頁面進行安裝。 在專案中引用 .DLL 檔案即可使用它。 產生 PDF 文件並列印作業 我們將從 URL 產生 PDF 文件。 建立 PDF 文件很簡單,通常只需兩個步驟。 以下程式碼範例產生 PDF 檔案: using IronPdf; // Initialize a new instance of ChromePdfRenderer. ChromePdfRenderer Renderer = new ChromePdfRenderer(); // Render the specified URL as a PDF document. PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/"); using IronPdf; // Initialize a new instance of ChromePdfRenderer. ChromePdfRenderer Renderer = new ChromePdfRenderer(); // Render the specified URL as a PDF document. PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/"); Imports IronPdf ' Initialize a new instance of ChromePdfRenderer. Private Renderer As New ChromePdfRenderer() ' Render the specified URL as a PDF document. Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/") $vbLabelText $csharpLabel 使用上述程式碼建立了一個 PDF 文檔對象,該對像已準備好進行列印。 接下來,我們將使用預設印表機將PDF 文件列印到紙張上。 這段程式碼只有一行,內容如下: // Print the PDF document using the default printer settings. Pdf.Print(); // Print the PDF document using the default printer settings. Pdf.Print(); ' Print the PDF document using the default printer settings. Pdf.Print() $vbLabelText $csharpLabel 此Print方式會將 PDF 檔案傳送至預設印表機進行列印。 進階列印選項 IronPDF 為靜默列印提供了多種進階列印選項。 使用PdfDocument.GetPrintDocument方法,並將結果儲存在System.Drawing.Printing.PrintDocument物件中。 程式碼很簡單,如下所示: // Remember to add assembly reference to System.Drawing.dll in project // Get the print document for the PDF. System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument(); // Remember to add assembly reference to System.Drawing.dll in project // Get the print document for the PDF. System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument(); ' Remember to add assembly reference to System.Drawing.dll in project ' Get the print document for the PDF. Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument() $vbLabelText $csharpLabel 指定印表機名稱 IronPDF 也提供了指定列印到特定印表機的功能。 若要指定名稱,可以使用PrinterSettings.PrinterName屬性。 首先,我們需要取得目前的 PDF 文檔物件。 程式碼範例如下: using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Send the print job to the specified printer. printDocument.Print(); } using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Send the print job to the specified printer. printDocument.Print(); } Using printDocument = pdfDocument.GetPrintDocument() ' Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF" ' Send the print job to the specified printer. printDocument.Print() End Using $vbLabelText $csharpLabel 設定印表機解析度 另一個很棒的功能是設定印表機解析度。 我們可以根據輸出結果控制要列印、顯示的像素數。 PDF 文件的DefaultPageSettings.PrinterResolution屬性可用於設定解析度。 以下是一個非常簡單的程式碼範例: // Set the custom resolution for the printer. printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution { Kind = PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Send the print job with the custom resolution settings. printDocument.Print(); // Set the custom resolution for the printer. printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution { Kind = PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Send the print job with the custom resolution settings. printDocument.Print(); ' Set the custom resolution for the printer. printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With { .Kind = PrinterResolutionKind.Custom, .X = 1200, .Y = 1200 } ' Send the print job with the custom resolution settings. printDocument.Print() $vbLabelText $csharpLabel PrintToFile 方法 PdfDocument類別提供了PrintToFile方法,讓我們在 C#中將 PDF 列印到檔案。 它接受pathToFile作為參數,以便將檔案直接列印到該位置,而無需打開印表機對話方塊。 程式碼很簡單,如下所示: // Print the document to a specified file location. printDocument.PrintToFile("PathToFile", false); // Print the document to a specified file location. printDocument.PrintToFile("PathToFile", false); ' Print the document to a specified file location. printDocument.PrintToFile("PathToFile", False) $vbLabelText $csharpLabel 完整的程式碼範例如下: using IronPdf; // Initialize the PDF renderer and create the PDF document. ChromePdfRenderer Renderer = new ChromePdfRenderer(); PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/"); using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Set a custom print resolution. printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution { Kind = PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Execute the print job. printDocument.Print(); } using IronPdf; // Initialize the PDF renderer and create the PDF document. ChromePdfRenderer Renderer = new ChromePdfRenderer(); PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/"); using (var printDocument = pdfDocument.GetPrintDocument()) { // Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Set a custom print resolution. printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution { Kind = PrinterResolutionKind.Custom, X = 1200, Y = 1200 }; // Execute the print job. printDocument.Print(); } Imports IronPdf ' Initialize the PDF renderer and create the PDF document. Private Renderer As New ChromePdfRenderer() Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/") Using printDocument = pdfDocument.GetPrintDocument() ' Specify the printer name. printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF" ' Set a custom print resolution. printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With { .Kind = PrinterResolutionKind.Custom, .X = 1200, .Y = 1200 } ' Execute the print job. printDocument.Print() End Using $vbLabelText $csharpLabel 程式碼執行後,會將 URL 轉換為 PDF 文件。 然後,要靜默列印 PDF 文檔,可以使用GetPrintDocument方法。 程式檔案編譯執行成功後,會出現一個印表機對話框,提示將其儲存為 PDF 文件。 然後使用指定的印表機名稱儲存 PDF 檔案。 摘要 在本文中,我們仔細研究如何使用 IronPDF 靜默列印 PDF 文件。 IronPDF 在 PDF 列印時提供了許多實用選項。 它還可以追蹤已列印的頁面,並允許您在指定的頁面範圍內進行列印。 IronPDF 的靜默列印功能以及其他列印選項使其成為 C# 中處理 PDF 時的一個傑出庫。 IronPDF 可以幫助使用者將不同格式的資料轉換為 PDF,以及將 PDF 轉換為其他格式。 它使開發人員能夠輕鬆地將 PDF 功能整合到應用程式開發流程中。 另外,檢視和編輯PDF文件不需要Adobe Acrobat Reader。 IronPDF 可免費用於個人開發,也可授權用於商業用途。 It provides a free trial license to access and test out the full functionality of the library. 您可以點擊此連結查看更多詳情。 常見問題解答 如何在 C# 中靜默列印 PDF? 您可以使用 IronPDF 的 Print 方法,在 C# 中默默地打印 PDF。這可讓您在不開啟印表機對話框的情況下進行列印,確保保留您的文件格式。 設定 IronPDF 進行 PDF 無聲列印需要哪些步驟? 若要設定 IronPDF 進行無聲 PDF 列印,請透過 NuGet 套件管理員安裝函式庫,或下載 .DLL 檔案。然後,使用 Print 方法設定靜音列印設定,並指定印表機屬性,例如 PrinterName 和 PrinterResolution 。 如何確保列印時維持 PDF 格式? IronPDF 可讓您設定特定的印表機設定,例如設定 PrinterResolution 屬性,以確保列印輸出與原始文件相符,從而協助維持 PDF 格式。 我可以用 C# 將 PDF 列印到檔案位置嗎? 是的,IronPDF 提供了 PrintToFile 方法,它允許您直接將 PDF 列印到指定的檔案位置,而無需開啟印表機對話框。 不使用 Adobe Acrobat Reader 是否可以列印 PDF? 是的,IronPdf 可讓您列印、檢視和編輯 PDF,而不需要 Adobe Acrobat Reader,使其成為在 C# 應用程式中處理 PDF 文件的靈活工具。 IronPDF 是否支援跨平台 PDF 列印? 是的,IronPdf 支援跨平台 PDF 列印,提供 Windows、macOS、Android 和 iOS 的相容性,使其適用於各種開發環境。 IronPDF 有哪些試用選項? IronPdf 對於個人開發是免費的,並提供試用授權,可讓您在承諾商業授權之前測試其完整功能。 如何使用 IronPDF 指定特定的印表機進行 PDF 列印? 您可以透過設定 IronPDF 中的 PrinterSettings.PrinterName 屬性來指定特定的印表機,將列印工作導向您所需的印表機。 使用 IronPDF 開發 C# 應用程式建議使用何種 IDE? Visual Studio 是使用 IronPDF 開發 C# 應用程式的推薦整合開發環境 (IDE),可為編碼和專案管理提供強大的平台。 如何排除 C# 中無聲 PDF 列印的問題? 如果您在 C# 中遇到 PDF 無聲列印的問題,請確保 IronPDF 已正確安裝,且 PrinterName 和 PrinterResolution 等印表機設定已正確配置。諮詢 IronPDF 文件也可以提供有用的疑難排解提示。 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 文件不顯示對話框如何在 C# 中不使用 Adobe 打...
發表日期 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 打印。靜默打印、對話框選項、自定義設置。 閱讀更多