使用 IRONPRINT 使用 IronPDF 在 C# 中打印 PDF 文件 Curtis Chau 更新:2026年2月10日 下載 IronPrint NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 IronPrint是 Iron Software 全新推出的.NET列印庫,相容於包括 Windows、macOS、Android 和 iOS 在內的多種平台。立即開始使用IronPrint ! PDF是最常用的文件格式。 它體積小巧,並且受到大多數作業系統的支援。 PDF檔案可以輕鬆地在不同裝置之間共用。 它們在任何介面和作業系統上看起來都完全一樣,因此閱讀起來非常方便。 使用者可以透過包括手機、平板電腦等在內的所有裝置存取 PDF 檔案。 由於其受歡迎程度,所有 C# 開發人員都必須知道如何使用 C# 列印 PDF 文件。 這是因為當您需要列印發票、薪資單、訂單或個人資料等文件時,可能會遇到問題。這些文件通常是 PDF 文件格式,因此我們必須知道如何使用 C# 列印 PDF 文件。 在 C# 中,可以使用多個庫來列印 PDF 文件。 但是,您可能會遇到的一個問題是許可證費用。 它們大多數都不是免費的,或者使用起來很困難。 但幸運的是,你不必擔心。 我找到了在 C# 中列印 PDF 文件的最簡單方法。 I will use the IronPDF Library to print PDF documents. 相信我,你一定會喜歡的! 你從這篇文章中能獲得什麼? 閱讀本文後,您將能夠完成以下任務: 使用 C# 列印單一 PDF 文檔 使用 C# 一次列印多個 PDF 文件 將網址轉換為PDF格式,然後列印出來。 為了更好地理解本教程,您必須具備 C# 程式設計的基礎知識。 在開始教學之前,我們先來了解IronPDF是什麼。 IronPDF: IronPDF是一個.NET函式庫,它為我們提供了一個最簡單的方法來讀取、建立、操作和列印 PDF 檔案。 該函式庫可用於 C# 和 VB .NET應用程式。 它可以輕鬆地與.NET、 .NET Core、MVC、Web Forms 和 Web API 在.NET和.NET Core上搭配使用。 它允許用戶下載PDF文件,透過電子郵件發送,並將其儲存在雲端。 您可以透過此連結了解更多關於IronPDF 的資訊。 讓我們開始示範。 開啟 Visual Studio 開啟 Visual Studio。 如果您要將此功能新增至現有軟體中,請建立新專案或開啟現有專案。 本教學使用控制台應用程式; 您可以根據軟體需求選擇任一方案。 項目已建立。 安裝NuGet包 Open the console by clicking on Tools > NuGet Package Manager > Package Manager 安慰 from the Menu bar on the top of the window. NuGet套件管理器控制台將會打開,如下所示: NuGet套件管理器控制台 Write the following command in the console to Install IronPdf NuGet Package and press Enter. Install-Package IronPrint NuGet套件將會如下所示進行安裝: 安裝NuGet包 Print PDF Filed in C 現在,讓我們來看一個例子,示範如何使用IronPDF庫在 C# 中列印 PDF 文件。 開啟 Program.cs 文件。在 Program.cs 檔案頂部新增以下命名空間以使用IronPDF: using IronPdf; using IronPdf; $vbLabelText $csharpLabel 現在,在 Main 函數中編寫以下程式碼: using System; // Required for 安慰 operations class Program { static void Main() { // Load the PDF document from file using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); // Print the loaded PDF document to the default printer pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); 安慰.ReadKey(); } } using System; // Required for 安慰 operations class Program { static void Main() { // Load the PDF document from file using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); // Print the loaded PDF document to the default printer pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); 安慰.ReadKey(); } } $vbLabelText $csharpLabel 讓我們來理解這段程式碼。 以下程式碼行會將 PDF 檔案載入到記憶體中。 "Sample.Pdf"是檔名,D盤是我的檔案位置。 您必須輸入完整的檔案路徑以及檔案名稱。 using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); $vbLabelText $csharpLabel Print() 函數將使用您的預設印表機列印已載入的檔案。 測試程式 按 Ctrl + F5 運行程式。 安慰 Print Multiple PDF Documents in C 我將向您展示如何使用IronPDF在 C# 中列印多個 PDF 文件。 14 個 PDF 文件 在這個例子中,我需要列印 14 個 PDF 文件。 我已將它們命名為 1 - 14。您可以將所有檔案儲存為 ID,也可以將檔案位置儲存在資料庫中。 從資料庫中提取檔案路徑和檔案名,並將它們儲存到數組或清單中。遍歷該數組或列表,列印出所有文件。 在 Main 函數內部寫入以下程式碼: using System; // Required for 安慰 operations class Program { static void Main() { // Loop through each file number and print for (int i = 1; i <= 14; i++) { // Load each PDF document from file using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf"); // Inform which file is being printed 安慰.WriteLine("Printing File: {0}.pdf", i); // Print the loaded PDF document to the default printer pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("{0}.pdf Printed Successfully!", i); } 安慰.ReadKey(); } } using System; // Required for 安慰 operations class Program { static void Main() { // Loop through each file number and print for (int i = 1; i <= 14; i++) { // Load each PDF document from file using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf"); // Inform which file is being printed 安慰.WriteLine("Printing File: {0}.pdf", i); // Print the loaded PDF document to the default printer pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("{0}.pdf Printed Successfully!", i); } 安慰.ReadKey(); } } $vbLabelText $csharpLabel 我使用 for 迴圈根據檔案名稱產生 1 到 14 的數字。 運行解決方案: 按 Ctrl + F5 運行程式。 安慰 將URL轉換為PDF並列印: 我將向您展示如何透過解析 URL 到IronPDF來列印網頁。 圖書館將透過解析 HTML 來建立 PDF 文件。 我們可以使用列印功能列印該PDF文件。 我們來看一個例子。 在主函數中寫入以下程式碼: using System; // Required for 安慰 operations class Program { static void Main() { // HTML to PDF Renderer IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf(); // Inform about the printing operation 安慰.WriteLine("Printing File"); // Render URL as PDF using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing"); // Print the PDF document PdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); } } using System; // Required for 安慰 operations class Program { static void Main() { // HTML to PDF Renderer IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf(); // Inform about the printing operation 安慰.WriteLine("Printing File"); // Render URL as PDF using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing"); // Print the PDF document PdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); } } $vbLabelText $csharpLabel 程式將解析 https://ironpdf.com/how-to/print-pdf/#advanced-printing 為 PDF,然後 Print() 函數將使用預設印表機列印 PDF。 讓我們運行一下程式。 運行程式: 按 Ctrl + F5 運行解決方案。 螢幕上顯示的輸出 Print() 函數將使用預設印表機列印文件。 有時我們可能不想使用預設印表機。 在這種情況下, IronPDF提供了一行程式碼來更改印表機名稱。 變更印表機名稱: 讓我們舉個例子來幫助我們更好地理解。 使用以下程式碼變更預設印表機: using System; // Required for 安慰 operations class Program { static void Main() { // Load the PDF document from file PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); // Change to a different printer by name pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Print the PDF document pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); } } using System; // Required for 安慰 operations class Program { static void Main() { // Load the PDF document from file PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf"); // Change to a different printer by name pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"; // Print the PDF document pdfDocument.Print(); // Print operation success message to console 安慰.WriteLine("File Printed Successfully!"); } } $vbLabelText $csharpLabel 您可以看到,我只添加了一行額外的程式碼來指定印表機。 我以 Microsoft Print to PDF 為例進行示範。 運行程式: 按 Ctrl + F5 運行程式。 接下來會出現一個對話框,要求我們輸入要儲存的檔名,因為我們使用的是 Microsoft Print to PDF。 指定檔案名,然後按一下"儲存"按鈕。 我已指定"SamplePrint.Pdf"。 控制台將顯示以下輸出: Microsoft Print to PDF 已在我的 D 盤中列印了一個 PDF 檔案。讓我們看看。 希望這篇教學對您有幫助,互動性強,而且容易理解。 您可以在下方評論區提出問題或提供回饋意見。 常見問題解答 如何使用 IronPDF 在 C# 中列印 PDF 文件? 要在 C# 中使用 IronPDF 列印 PDF 文件,請使用 PdfDocument.FromFile 方法加載 PDF 文檔,然後對加載的文檔調用 Print 方法。 IronPDF 能否按順序列印多個 PDF 文件? 可以,IronPDF 可以通過遍歷文件路徑集合並對每個文檔調用 Print 方法來列印多個 PDF 文件。 如何在 C# 中將 URL 轉換為 PDF 並列印? 使用 IronPDF,通過 HtmlToPdf.RenderUrlAsPdf 方法將 URL 轉換為 PDF,然後對生成的 PDF 文檔使用 Print 方法進行列印。 能否選擇特定打印機來使用 IronPDF 列印 PDF? 可以,您可以在使用 PdfDocument 類的 GetPrintDocument 方法時,通過在 PrinterSettings 對象中設置 PrinterName 屬性來選擇特定的打印機。 IronPDF 支持哪些平台的 PDF 列印? IronPDF 支持在 .NET Framework、.NET Core 和 .NET 5 和 6 的應用程式上進行 PDF 列印,並與 Windows、macOS、Android 和 iOS 平台兼容。 如何開始使用 .NET 庫在 C# 中列印 PDF? 要開始使用 IronPDF 列印 PDF,請通過 Visual Studio 中的 NuGet 套件管理器安裝此庫,並遵循 Iron Software 提供的文檔和範例。 使用 PDF 分享文檔的好處是什麼? PDF 具有重量輕、在不同設備中保持一致格式並被大多數操作系統支持的優點。 我是否需要高級 C# 技能才能使用 IronPDF 進行列印? 使用 IronPDF 進行列印任務只需基本的 C# 知識,因為該庫提供簡單的方法和示例以幫助開發人員。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新2026年2月27日 如何使用IronPrint在VB.NET中不使用Adobe列印PDF VB.NET列印PDF教程:學習靜默列印、對話框選項、自定義列印設定和打印機管理,無需Adobe。 閱讀更多 更新2025年12月19日 使用 IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多 更新2026年1月25日 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多 如何在 .NET Core 中打印 PDF 文件
更新2026年2月27日 如何使用IronPrint在VB.NET中不使用Adobe列印PDF VB.NET列印PDF教程:學習靜默列印、對話框選項、自定義列印設定和打印機管理,無需Adobe。 閱讀更多
更新2025年12月19日 使用 IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多
更新2026年1月25日 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 應用中實現 PDF 打印。靜默打印、對話框選項、自定義設置。 閱讀更多