使用 IronPDF 在 C# 中打印 PDF 文件
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 應用程式。 它能輕鬆與 ASP.NET、ASP.NET core、MVC、Web Forms 以及 .NET 和 .NET core 上的 Web API 搭配使用。 它允許用戶下載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 Package Manager 命令列
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;
Imports IronPdf
現在,在 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();
}
}
Imports System ' Required for 命令列 operations
Class Program
Shared Sub Main()
' Load the PDF document from file
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
' Print the loaded PDF document to the default printer
pdfDocument.Print()
End Using
' Print operation success message to console
命令列.WriteLine("File Printed Successfully!")
命令列.ReadKey()
End Sub
End Class
讓我們來理解這段程式碼。
以下程式碼行會將 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
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();
}
}
Imports System ' Required for 命令列 operations
Class Program
Shared Sub Main()
' Loop through each file number and print
For i As Integer = 1 To 14
' Load each PDF document from file
Using pdfDocument As 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)
End Using
Next
命令列.ReadKey()
End Sub
End Class
我使用 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!");
}
}
Imports System ' Required for 命令列 operations
Class Program
Shared Sub Main()
' HTML to PDF Renderer
Dim Renderer As New IronPdf.HtmlToPdf()
' Inform about the printing operation
命令列.WriteLine("Printing File")
' Render URL as PDF
Using PdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing")
' Print the PDF document
PdfDocument.Print()
End Using
' Print operation success message to console
命令列.WriteLine("File Printed Successfully!")
End Sub
End Class
此程式將解析 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!");
}
}
Imports System ' Required for 命令列 operations
Class Program
Shared Sub Main()
' Load the PDF document from file
Dim pdfDocument As 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!")
End Sub
End Class
您可以看到,我只添加了一行額外的程式碼來指定印表機。 我以 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# 知識,因為該庫提供簡單的方法和示例以幫助開發人員。


