跳過到頁腳內容
產品比較

Itextsharp 和 IronPDF 之間的 C# 中的 PDF 打印比較

使用 C# 列印 PDF 文件是許多桌面和 Web 應用程式的常見需求。 透過程式設計方式產生和列印 PDF 文件,開發人員在處理文件列印任務時擁有更大的靈活性。 在 C# 中,開發人員可以使用各種函式庫和 API,輕鬆產生和列印 PDF 文件。 無論是列印發票、收據或報告,能夠動態產生 PDF 文件並直接從應用程式列印,都能節省時間和精力。 本文探討如何使用 C# 以及一些可用於此目的的流行函式庫和 API 來列印 PDF。

為此,我們將使用以下程式庫並進行比較。

  1. IronPDF
  2. iTextSharp

在本文中,我們將探討如何使用 IronPDF 和 ITextSharp 列印 PDF 文件。這兩者都是高級 PDF 操作 API。

1.IronPDF 2.

IronPDF是一個功能強大的 C# 庫,它允許開發人員輕鬆建立、操作和處理 PDF 文件。 它提供了一系列功能,使其成為開發人員的熱門選擇,包括將 HTML、CSS 和圖像轉換為 PDF、從頭開始生成 PDF 以及編輯現有 PDF。 IronPDF 也支援各種文件元素,例如圖像、文字、表格和表單,並提供數位簽章、浮水印和加密等進階功能。 它用戶友好的介面和全面的文檔使其成為希望將 PDF 功能整合到其應用程式中的 .NET 開發人員的熱門選擇。

2. iTextSharp

iText 是一個流行的開源 PDF 函式庫,開發人員可以使用它在 Java 和 .NET 環境中建立、操作 PDF 文件和從中提取資料。 iTextSharp 是 iText 函式庫在 iText 5 發布後停止維護之前,從原 iText 函式庫移植而來的 .NET 版本。 iText 7 是該庫的最新版本,它完全重寫了原始 iText 庫,提供了更多功能、更好的效能和更強的可擴展性。 iText 7 提供一系列功能,包括 PDF 產生和操作 PDF 文件、數位簽章、表單填寫以及建立 PDF 的支援。 它廣泛應用於金融、法律、政府等各個行業,並以其高品質的產出和靈活性而聞名。

3. 安裝 IronPDF 庫

以下是使用 NuGet 套件管理器安裝 IronPDF 庫的步驟:

  1. 開啟 Visual Studio 並建立一個新專案或開啟一個現有專案。
  2. 在解決方案資源管理器中以滑鼠右鍵按一下項目,然後選擇"管理 NuGet 套件"。

    1

3.在"NuGet 套件管理員"視窗中,選擇"瀏覽"標籤。

  1. 在搜尋欄中輸入"IronPDF",然後按 Enter 鍵。
  2. 從搜尋結果中選擇"IronPDF",然後點選"安裝"按鈕。

    ! IronPDF NuGet 包搜索

  3. 查看將要安裝的依賴項,然後按一下"確定"繼續安裝。
  4. 等待安裝完成。 您可以在"輸出"視窗中追蹤進度。
  5. 安裝完成後,IronPDF 庫將會加入您的專案中,這樣您就可以開始使用 IronPDF 了。

就是這樣! 您已使用 NuGet 套件管理器成功安裝了 IronPDF。 您也可以直接從 IronPDF 網站下載 IronPDF,或是下載 dll 檔案手動安裝。

4. 安裝 iTextsharp PDF 庫

以下是使用 NuGet 套件管理器安裝 iTextsharp 的步驟:

  1. 開啟 Visual Studio,建立一個新專案或開啟一個現有專案。
  2. 在解決方案資源管理器中以滑鼠右鍵按一下項目,然後選擇"管理 NuGet 套件"。

    3

3.在"NuGet 套件管理員"視窗中,選擇"瀏覽"標籤。

  1. 在搜尋欄中輸入"iTextsharp",然後按 Enter 鍵。
  2. 從搜尋結果中選擇"iText 7",然後點選"安裝"按鈕。

    4

  3. 查看將要安裝的依賴項,然後按一下"確定"繼續安裝。
  4. 等待安裝完成。 您可以在"輸出"視窗中追蹤進度。
  5. 安裝完成後,您就可以在專案中使用 iTextsharp 了。

就是這樣! 您已使用 NuGet 套件管理器成功安裝了 iTextsharp。

5. 使用 IronPDF 列印 PDF 文檔

使用 IronPDF 列印 PDF 文件非常簡單,它提供了許多選項,只需幾行程式碼即可列印 PDF 文件。 下面,我們將討論一個程式碼範例,該範例僅使用 IronPDF 的渲染 PDF 物件來列印 IronPDF tiger wiki 頁面,並將指令傳送到預設印表機,而無需任何其他程式庫。

using IronPdf;

// Initialize ChromePdfRenderer to render a URL as PDF
var renderer = new ChromePdfRenderer();

// Render the PDF from a URL
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Print the PDF at 300 DPI to the default printer
pdf.Print(300, true);

// Get a PrintDocument for additional customization if needed
System.Drawing.Printing.PrintDocument printDocYouCanWorkWith = pdf.GetPrintDocument();
using IronPdf;

// Initialize ChromePdfRenderer to render a URL as PDF
var renderer = new ChromePdfRenderer();

// Render the PDF from a URL
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Print the PDF at 300 DPI to the default printer
pdf.Print(300, true);

// Get a PrintDocument for additional customization if needed
System.Drawing.Printing.PrintDocument printDocYouCanWorkWith = pdf.GetPrintDocument();
Imports IronPdf

' Initialize ChromePdfRenderer to render a URL as PDF
Private renderer = New ChromePdfRenderer()

' Render the PDF from a URL
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")

' Print the PDF at 300 DPI to the default printer
pdf.Print(300, True)

' Get a PrintDocument for additional customization if needed
Dim printDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = pdf.GetPrintDocument()
$vbLabelText   $csharpLabel

以上程式碼示範如何從 URL 取得資料,IronPDF 使用該 URL 建立 PDF 檔案。然後,程式碼設定印表機參數,並將 PDF 檔案傳送到預設印表機以直接列印該 PDF 文件。

6. 使用 iTextSharp 列印 PDF 文件

使用 iTextSharp 建立 PDF 文件非常容易,但是列印 PDF 文件時,則需要使用其他第三方函式庫。

using iText.Kernel.Pdf;
using System.Drawing.Printing;

// Method to print a PDF using iTextSharp and another library (e.g., PrintDocument)
public void PrintPDF(string filePath) {
    // Create a PdfReader to read the PDF file
    PdfReader reader = new PdfReader(filePath);

    // Create a PdfDocument to interact with the PDF data
    PdfDocument pdfDoc = new PdfDocument(reader);

    // Assuming there exists a PrintHelper class that can handle PDF printing
    PrintHelper printHelper = new PrintHelper(pdfDoc);

    // Print the PDF document using the helper
    printHelper.Print();

    // Close the PdfDocument to release resources
    pdfDoc.Close();
}    
using iText.Kernel.Pdf;
using System.Drawing.Printing;

// Method to print a PDF using iTextSharp and another library (e.g., PrintDocument)
public void PrintPDF(string filePath) {
    // Create a PdfReader to read the PDF file
    PdfReader reader = new PdfReader(filePath);

    // Create a PdfDocument to interact with the PDF data
    PdfDocument pdfDoc = new PdfDocument(reader);

    // Assuming there exists a PrintHelper class that can handle PDF printing
    PrintHelper printHelper = new PrintHelper(pdfDoc);

    // Print the PDF document using the helper
    printHelper.Print();

    // Close the PdfDocument to release resources
    pdfDoc.Close();
}    
Imports iText.Kernel.Pdf
Imports System.Drawing.Printing

' Method to print a PDF using iTextSharp and another library (e.g., PrintDocument)
Public Sub PrintPDF(ByVal filePath As String)
	' Create a PdfReader to read the PDF file
	Dim reader As New PdfReader(filePath)

	' Create a PdfDocument to interact with the PDF data
	Dim pdfDoc As New PdfDocument(reader)

	' Assuming there exists a PrintHelper class that can handle PDF printing
	Dim printHelper As New PrintHelper(pdfDoc)

	' Print the PDF document using the helper
	printHelper.Print()

	' Close the PdfDocument to release resources
	pdfDoc.Close()
End Sub
$vbLabelText   $csharpLabel

上面的程式碼從指定路徑檢索 PDF 文件,建立一個新的 PDF 文檔對象,然後使用輔助類別將文檔傳送到預設印表機進行列印。 請注意,iTextSharp 本身並不支援列印功能,因此需要額外的第三方程式庫或程式碼來處理實際的列印作業過程。

7.結論

使用 C# 列印 PDF 文件是許多桌面和 Web 應用程式的基本功能。 IronPDF和 iTextSharp 是兩個流行的庫,可以輕鬆建立、操作和列印 PDF 文件。 這兩個庫都提供了一系列功能,包括將 HTML、CSS 和圖像轉換為 PDF,編輯現有 PDF,以及添加數位簽名、浮水印和加密。 IronPDF 以其用戶友好的介面和全面的文件而聞名,使其成為 .NET 開發人員的熱門選擇。 另一方面,iTextSharp 也提供了許多 PDF 功能。 但值得注意的是,它不提供 PDF 列印功能。 為了實現這一目標,我們將不得不使用其他第三方工具。

5

要了解有關使用 IronPDF 列印 PDF 的更多信息,請訪問以下連結

如需全面比較 IronPDF 和 iTextSharp,請造訪此連結

請注意iTextSharp 是其各自所有者的註冊商標。 本網站與 iTextSharp 無任何關聯、背書或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,反映了撰寫時的公開信息。

常見問題解答

如何使用 C# 列印 PDF 文件?

您可以通過利用像 IronPDF 這樣的庫來使用 C# 列印 PDF 文件,其提供了一種簡單的方法來使用其渲染的 PDF 對象將 PDF 發送到預設打印機。

使用 IronPDF 列印 PDF 有何優勢?

IronPDF 在列印 PDF 方面提供了多個優勢,包括易用性、完整的文檔以及直接列印而不需要附加的第三方工具的能力。它還支持將 HTML、CSS 和圖像轉換為 PDF 的各種特性。

如何在 C# 項目中安裝 IronPDF?

要在 C# 項目中安裝 IronPDF,請在 Visual Studio 中使用 NuGet 套件管理器。右鍵單擊您的項目,選擇 '管理 NuGet 套件',搜索 'IronPDF',然後點擊 '安裝'。

iTextSharp 能否直接在 C# 中列印 PDF?

iTextSharp 不支持直接原生 PDF 列印。它需要額外的第三方庫或自訂代碼來處理打印任務。

IronPDF 和 iTextSharp 在 PDF 操作方面的主要區別是什麼?

主要區別在於 IronPDF 提供直接支持列印 PDF 且包含 HTML 轉 PDF 以及編輯現有 PDF 的功能。iTextSharp 雖然具備靈活性且提供高品質的輸出,但缺乏直接列印支持,這需要額外的工具來實現此功能。

為什麼在 C# 應用程序中 PDF 列印很重要?

在 C# 應用程序中,PDF 列印對於生成發票、收據和報告等任務來說至關重要。它使開發人員能夠生成跨平台保持一致格式的專業文檔。

IronPDF 列印問題的常見故障排除步驟是什麼?

常見的故障排除步驟是確保已安裝最新版本的 IronPDF 並正確配置項目依賴性。檢查打印機設定和錯誤日誌也有助於診斷問題。

如何在 C# 中使用 IronPDF 將 HTML 轉換為 PDF?

您可以使用IronPDF的RenderHtmlAsPdf方法將HTML字串轉換為PDF。此外,可以使用RenderHtmlFileAsPdf方法將HTML文件轉換為PDF。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我