跳至頁尾內容
產品比較

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

使用 C# 列印 PDF 文件是許多桌面和網頁應用程式中的常見需求。 程式化生成和列印 PDF 的能力,讓開發者在處理文件列印工作中更具彈性。 在 C# 中,開發者可以輕鬆使用多種程式庫和 API 來生成和列印 PDF 文件。 無論是列印發票、收據還是報告,動態生成 PDF 文件並直接從應用程式中列印,節省了時間和精力。 本文探討如何使用 C# 列印 PDF,以及為此目的而提供的一些流行程式庫和 API。

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

  1. IronPDF
  2. iText

在本文中,我們將了解如何使用 IronPDF 和 iText 列印 PDF 文件。 這兩者都是先進的 PDF 操作 API。

1. IronPDF

IronPDF 是一個強大的 C# 程式庫,讓開發者可以輕鬆建立、操作和處理 PDF 文件。 它提供了豐富的功能,使其成為開發者中的熱門選擇,包括將 HTML、CSS 和圖像轉換為 PDF、從頭生成 PDF 並編輯現有 PDF。 IronPDF 還支援多種文件元素,如圖像、文字、表格和表單,並提供數位簽名、水印和加密等高級功能。 其使用者友好的介面和詳盡的文件使其成為希望在應用程式中整合 PDF 功能的 .NET 開發者的首選。

2. iText

iText 是一個流行的開源 PDF 程式庫,開發者使用它在 Java 和 .NET 環境中建立、操作和提取 PDF 文件中的資料。 iText 是從原始的 iText 程式庫移植到 .NET 的,但在 iText 發布後已停止支援。 iText 是該程式庫的最新版本,完全重寫了原始的 iText 程式庫,提供了更多功能、更好的性能和改進的擴展性。 iText 提供了一系列功能,包括 PDF 生成和操作 PDF 文件、數位簽名、表單填寫及支援建立 PDF 文件。 它廣泛應用於各種行業,包括金融、法律和政府,因其高品質輸出和靈活性而聞名。

3. 安裝 IronPDF 程式庫

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

  1. 打開 Visual Studio,建立一個新專案或打開現有專案。
  2. 右鍵點擊解決方案總管中的專案,選擇"管理 NuGet 套件"。

    1

  3. 在"NuGet 套件管理器"視窗中,選擇"瀏覽"標籤。
  4. 在搜索欄輸入"IronPDF"並按下回車。
  5. 從搜索結果中選擇"IronPDF",然後按"安裝"按鈕。

    IronPDF NuGet package Search

  6. 查看將要安裝的依賴項,並按"OK"進行安裝。
  7. 等待安裝完成。 您可以在"輸出"視窗中查看進度。
  8. 安裝完成後,IronPDF 程式庫將已新增到您的專案中,您可以開始使用 IronPDF。

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

4. 安裝 iText PDF 程式庫

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

  1. 打開 Visual Studio,建立或打開現有專案。
  2. 右鍵點擊解決方案總管中的專案,選擇"管理 NuGet 套件"。

    3

  3. 在"NuGet 套件管理器"視窗中,選擇"瀏覽"標籤。
  4. 在搜索欄輸入"iText"並按下回車。
  5. 從搜索結果中選擇"iText",然後按"安裝"按鈕。

    4

  6. 查看將要安裝的依賴項,並按"OK"進行安裝。
  7. 等待安裝完成。 您可以在"輸出"視窗中查看進度。
  8. 安裝完成後,您可以開始在專案中使用 iText。

完成了! 您已成功使用 NuGet 套件管理器安裝 iText。

5. 使用 IronPDF 列印 PDF 文件

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

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

上面的程式碼展示了如何從 IronPDF 用於建立 PDF 文件的 URL 獲取資料。接著,設置列印機參數並將 PDF 文件發送到預設列印機以直接列印 PDF 文件。

6. 使用 iText 列印 PDF 文件

使用 iText 建立 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 文件物件,然後使用輔助類將文件發送到預設列印機進行列印。 請注意,iText 本身不支持列印功能,所以需要額外的第三方程式庫或程式碼來處理實際的列印作業。

7. 結論

使用 C# 列印 PDF 文件是許多桌面和網頁應用程式的基本功能。 IronPDF 和 iText 是兩個流行的程式庫,提供簡單的建立、操作和列印 PDF 文件功能。 兩個程式庫都提供一系列功能,包括將 HTML、CSS 和圖像轉換為 PDF、編輯現有的 PDF 以及新增數位簽名、水印和加密。 IronPDF 因其使用者友好的介面和詳盡的文件而受到 .NET 開發者的歡迎。 另一方面,iText 也提供了許多 PDF 功能。 值得注意的是,它並不提供 PDF 列印功能。 要實現這一點,我們將不得不使用其他第三方工具。

5

要了解更多關於使用 IronPDF 列印 PDF 的資訊,請存取以下連結

關於 IronPDF 和 iText 的完整比較,請存取此連結

請注意iText 是其相關所有者的註冊商標。 本網站與 iText 無關,亦未得到其認可或贊助。 所有產品名稱、標誌和品牌均為其相應所有者的財產。 比較僅供資訊用途,並反映了撰寫時的公開可用資訊。)]

常見問題

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

您可以透過使用如IronPDF的程式庫來使用C#列印PDF文件,IronPDF提供了一個簡單的方法,使用其渲染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,雖然靈活且提供高品質輸出,但缺乏直接列印支援,需額外工具來實現此功能。

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

PDF列印在C#應用中很重要,用於生成發票、收據和報告等任務。這使得開發者能夠生成外觀專業且在不同平台上保持一致格式的文件。

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

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

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

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

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

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