使用 IRONPRINT

使用 IronPDF 列印 PDF 檔案的 C#代碼

發佈 2021年12月7日
分享:

PDF 是最常用的文件格式。它輕便且受到大多數操作系統的支持。PDF 文件可以輕鬆地從一個設備分享到另一個設備。它們提供了易於閱讀的體驗,因為它們在任何界面和操作系統上都會顯示得完全相同。PDF 可以在包括手機、平板電腦等所有設備上訪問。

它們的普及要求所有 C# 開發人員都知道如何使用 C# 打印 PDF 文件。這是因為您可能會遇到要打印文件的問題,例如發票、工資單、訂單單或簡歷等。這些文件通常是 PDF 格式的,因此我們必須知道如何使用 C# 打印 PDF 文件。

使用 C# 打印 PDF 文件可以使用多個庫來完成。但您可能遇到的一個問題是許可費。大多數庫不是免費的,或者難以使用。但幸運的是,您不必擔心。我找到了使用 C# 打印 PDF 文件最簡單、最容易的方法。

我將使用 IronPDF 用於列印 PDF 文件的程式庫。相信我,您會喜歡的。!


您將從本文中獲得什麼?

閱讀本文後,您將能夠執行以下任務:

  • 在C#中列印單一PDF文件
  • 使用 C# 一次列印多個 PDF 文件
  • 將 URL 轉換為 PDF 然後列印。

要更好地理解本教程,你必須具備基本的 C# 編程知識。

在開始本教程之前,讓我們先來理解一下什麼是 IronPDF。


IronPDF:

IronPDF 是一個 .NET 函式庫,提供我們最簡單的方法來讀取、創建、操作和 列印 PDF 檔案該函式庫可用於 C# 和 VB .NET 應用程式。它可輕鬆與 ASP .NET、ASP .NET core、MVC、Web Forms 和 Web API 在 .NET 和 .NET core 上一起運作。它允許用戶下載 PDF 文件,通過電子郵件發送並存儲在雲端。

您可以使用此 探索更多關於 IronPDF 的信息。 連結讓我們開始演示。

打開 Visual Studio

打開 Visual Studio。建立一個新專案,或如果您要將此功能添加到現有軟體中,則打開現有專案。

在本教程中,我使用主控台應用程式;您可以根據軟體需求選擇使用任何其他選項。

專案已建立

專案已建立。

安裝 NuGet 套件

打開控制台,點擊 工具 > NuGet 套件管理員 > 套件管理主控台 從視窗頂端的功能表中。

NuGet 套件管理器主控台將如下圖所示打開:

NuGet 套件管理器主控台

NuGet 套件管理器主控台

在控制台中輸入以下命令以安裝 IronPdf NuGet 套件 然後按下 Enter。

Install-Package IronPrint

NuGet 套件將按如下所示安裝:

安裝 NuGet 套件

安裝 NuGet 套件


使用 C# 列印 PDF 檔案

現在,我們來看一個示例,演示如何使用 IronPDF 庫在 C# 中列印 PDF 文件。

打開 Program.cs 檔案。在 Program.cs 檔案的頂部添加以下命名空間 using IronPDF:

現在,請寫下以下內容 代碼 在主函數內:

using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
    pdfDocument.Print();
    Console.WriteLine("File Printed Successfully!");
    Console.ReadKey();
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
    pdfDocument.Print();
    Console.WriteLine("File Printed Successfully!");
    Console.ReadKey();
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
		pdfDocument.Print()
		Console.WriteLine("File Printed Successfully!")
		Console.ReadKey()
End Using
VB   C#

讓我們來了解一下這段程式碼。

以下這行程式碼將會把 PDF 檔案加載到記憶體中。“Sample.Pdf” 是檔案名稱,而 D 磁碟是我的檔案位置。你需要寫上包含檔案名稱的完整檔案路徑。

using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
End Using
VB   C#

印刷()函式將使用預設印表機列印載入的檔案。

測試程式

按 Ctrl + F5 執行程式。

主控台

主控台


使用 C# 打印多個 PDF 文件

我將向您展示如何使用 IronPDF 在 C# 中打印多個 PDF 文件。

14 個 PDF 檔案

14 個 PDF 檔案

以下是這個範例所需的14個PDF文件,我已將它們命名為1-14。在此例中,您可以用ID來保存所有檔案,或者可以將檔案位置保存在資料庫中。從資料庫中提取檔案路徑和檔名,並將它們保存在數組或清單中。遍歷該數組或清單來打印所有檔案。

在主函數內寫下以下代碼:

for(int i = 1; i <= 14; i ++)
            {
                using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Print\"+i+".pdf");
                Console.WriteLine("Printing File: {0}.pdf",i);
                pdfDocument.Print();
                Console.WriteLine("{0}.pdf Printed Successfully!", i);
            }
            Console.ReadKey();
for(int i = 1; i <= 14; i ++)
            {
                using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Print\"+i+".pdf");
                Console.WriteLine("Printing File: {0}.pdf",i);
                pdfDocument.Print();
                Console.WriteLine("{0}.pdf Printed Successfully!", i);
            }
            Console.ReadKey();
For i As Integer = 1 To 14
				Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Print\" & i & ".pdf")
					Console.WriteLine("Printing File: {0}.pdf",i)
					pdfDocument.Print()
					Console.WriteLine("{0}.pdf Printed Successfully!", i)
				End Using
Next i
			Console.ReadKey()
VB   C#

我使用了一個 for 迴圈根據我的檔案名稱生成了從 1 到 14 的數字。

執行解決方案:

按 Ctrl + F5 執行程序。

主控台

主控台

將 URL 轉換為 PDF 然後列印:

我將向您展示如何通過將 URL 解析為 IronPDF 來列印網頁。該庫將通過解析 HTML 創建 PDF 文件。我們可以使用列印功能列印這個 PDF 文件。讓我們考慮一個例子。

在主函數中寫入以下代碼:

IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
            Console.WriteLine("Priniting File");
            using PdfDocument Pdfdoument = 
Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/csharp-print-pdf/#advanced-printing");
            Pdfdoument.Print();
            Console.WriteLine("File Printed Successfully!");
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
            Console.WriteLine("Priniting File");
            using PdfDocument Pdfdoument = 
Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/csharp-print-pdf/#advanced-printing");
            Pdfdoument.Print();
            Console.WriteLine("File Printed Successfully!");
Dim Renderer As New IronPdf.HtmlToPdf()
			Console.WriteLine("Priniting File")
			Using Pdfdoument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/csharp-print-pdf/#advanced-printing")
				Pdfdoument.Print()
				Console.WriteLine("File Printed Successfully!")
			End Using
VB   C#

此程式將解析 https://ironpdf.com/how-to/csharp-print-pdf/#advanced-printing 到 PDF 並列印()` function 將使用預設印表機列印PDF。

讓我們執行這個程式。

執行程式:

按下 Ctrl + F5 來執行解決方案。

顯示在螢幕上的輸出

顯示在螢幕上的輸出

印刷()` function將使用預設印表機列印文件。也許會遇到我們不想使用預設印表機的情況。在這種情況下,IronPDF提供了一行代碼來更改印表機名稱。

更改打印機名稱:

讓我們用一個例子來進一步了解。請使用以下 代碼 更改預設印表機:

PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
Dim pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
			pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"
			pdfDocument.Print()
			Console.WriteLine("File Printed Successfully!")
VB   C#

您可以看到我只添加了一行附加程式碼來指定打印機。我已經指定了 Microsoft Print to PDF 來進行演示。

執行程式:

按 Ctrl + F5 執行程式。

將會出現以下對話框,詢問我們輸入檔案名稱以進行儲存,因為我們使用 Microsoft Print to PDF。

指定檔案名稱並點擊“保存”按鈕。我已指定“SamplePrint.Pdf”。以下控制台輸出將顯示:

Microsoft Print to PDF 已經在我的 D 盤中打印了一個 PDF 文件。我們來看看吧。

希望這個教程對您有所幫助、互動性強且易於理解。您可以在下方的評論區提出問題或反饋意見。

< 上一頁
如何在 .NET Core 中列印 PDF 檔案

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 9,531 查看許可證 >