跳過到頁腳內容
產品比較

iText7在C#中讀取PDF的替代項(vs IronPDF)

PDF是一種由Adobe Acrobat Reader創建的可攜式文檔格式,廣泛用於在互聯網上共享信息。 它保留了數據的格式,並提供設置安全權限和密碼保護等功能。 作為C#開發人員,您可能遇到過需要將PDF功能集成到您的軟件應用程序中的情況。 從頭開始構建可能是一個耗時且乏味的任務。 因此,考慮應用程序的性能、效果和效率,在從頭創建新服務或使用預構建庫之間的權衡顯得尤為重要。

有幾個可用於C#的PDF庫。 在本文中,我們將探索兩個用於在C#中讀取PDF文檔的最受歡迎的PDF庫。

iText軟件

iText 7,以前稱為iText 7 Core,是一個用於在.NET C#和Java中編程PDF文檔的PDF庫。 它作為開源許可(AGPL)提供,並可以為商業應用程序授權。

iText Core是一個高級API,提供簡便的方法來生成和編輯PDF。 使用iText 7 Core,您可以拆分、合併、註釋、填寫表單、數字簽名,及在PDF文件上執行更多操作。 iText 7提供HTML到PDF轉換器

IronPDF

了解更多有關IronPDF的信息是一個.NET和.NET Framework C#和Java API,用於從URL、HTML文件或HTML字符串生成PDF文檔。 IronPDF允許您操作現有的PDF文件,如拆分、合併、註釋、數字簽名等等。

IronPDF擁有超過50項功能來創建、讀取和編輯PDF文件。 當您需要生成高質量、像素完美的專業PDF文件以使用Adobe Acrobat Reader時,它優先考慮速度、易用性和準確性。 API具有良好的文檔記錄,並且在其代碼範例頁面上可以找到許多樣本源代碼。

創建控制台應用程序

我們將使用Visual Studio 2022 IDE來創建應用程序作為開始。 Visual Studio是C#開發的官方IDE,您必須安裝它。 如果未安裝,您可以從Microsoft Visual Studio網站下載。

以下步驟將創建一個名為"DemoApp"的新項目。

  1. 打開Visual Studio,然後點擊"創建新項目"。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 1 - New project

  1. 選擇"控制台應用程序",然後點擊"下一步"。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 2

  1. 設定項目名稱。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 3

  1. 選擇.NET版本。 選擇穩定的版本.NET 6.0。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 4

安裝IronPDF庫

創建項目後,需要在項目中安裝IronPDF庫以便使用。 按照以下步驟安裝。

  1. 從解決方案資源管理器或工具中打開NuGet包管理器。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 5

  1. 瀏覽IronPDF庫並將其選中作為當前項目。 點擊安裝。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 6

在Program.cs文件的頂部添加以下命名空間:

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

安裝iText 7庫

創建項目後,需要在項目中安裝iText 7庫以便使用。 按照步驟安裝。

  1. 從解決方案資源管理器或工具中打開NuGet包管理器。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 7

  1. 瀏覽iText 7庫並將其選中作為當前項目。 點擊安裝。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 8

在Program.cs文件的頂部添加以下命名空間:

using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf;
Imports iText.Kernel.Pdf.Canvas.Parser.Listener
Imports iText.Kernel.Pdf.Canvas.Parser
Imports iText.Kernel.Pdf
$vbLabelText   $csharpLabel

打開PDF文件

我們將使用以下PDF文件提取文本。 這是一個兩頁的PDF文檔。

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 9

使用iText庫

使用iText庫打開PDF文件是一個兩步驟過程。 首先,我們創建一個PdfReader對象並將文件位置作為參數傳遞。 然後我們使用PdfDocument類創建一個新的PDF文檔。 代碼如下:

// Initialize a reader instance by specifying the path of the PDF file
PdfReader pdfReader = new PdfReader("sample.pdf");

// Initialize a document instance using the PdfReader
PdfDocument pdfDoc = new PdfDocument(pdfReader);
// Initialize a reader instance by specifying the path of the PDF file
PdfReader pdfReader = new PdfReader("sample.pdf");

// Initialize a document instance using the PdfReader
PdfDocument pdfDoc = new PdfDocument(pdfReader);
' Initialize a reader instance by specifying the path of the PDF file
Dim pdfReader As New PdfReader("sample.pdf")

' Initialize a document instance using the PdfReader
Dim pdfDoc As New PdfDocument(pdfReader)
$vbLabelText   $csharpLabel

使用 IronPDF

使用IronPDF打開PDF文件很簡單。 使用PdfDocument類的FromFile方法從任何文件位置打開PDF。 以下一行代碼打開一個PDF文件以讀取數據:

// Open a PDF file using IronPDF and create a PdfDocument instance
var pdf = PdfDocument.FromFile("sample.pdf");
// Open a PDF file using IronPDF and create a PdfDocument instance
var pdf = PdfDocument.FromFile("sample.pdf");
' Open a PDF file using IronPDF and create a PdfDocument instance
Dim pdf = PdfDocument.FromFile("sample.pdf")
$vbLabelText   $csharpLabel

從PDF文件讀取數據

使用iText7庫

在iText 7庫中讀取PDF數據並不那麼直觀。 我們必須手動循環遍歷PDF文檔的每一頁以從每頁提取文本。 以下源代碼幫助逐頁提取PDF文檔中的文本:

// Iterate through each page and extract text
for (int page = 1; page <= pdfDoc.GetNumberOfPages(); page++)
{
    // Define the text extraction strategy
    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

    // Extract text from the current page using the strategy
    string pageContent = PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(page), strategy);

    // Output the extracted text to the console
    Console.WriteLine(pageContent);
}

// Close document and reader to release resources
pdfDoc.Close();
pdfReader.Close();
// Iterate through each page and extract text
for (int page = 1; page <= pdfDoc.GetNumberOfPages(); page++)
{
    // Define the text extraction strategy
    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

    // Extract text from the current page using the strategy
    string pageContent = PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(page), strategy);

    // Output the extracted text to the console
    Console.WriteLine(pageContent);
}

// Close document and reader to release resources
pdfDoc.Close();
pdfReader.Close();
' Iterate through each page and extract text
Dim page As Integer = 1
Do While page <= pdfDoc.GetNumberOfPages()
	' Define the text extraction strategy
	Dim strategy As ITextExtractionStrategy = New SimpleTextExtractionStrategy()

	' Extract text from the current page using the strategy
	Dim pageContent As String = PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(page), strategy)

	' Output the extracted text to the console
	Console.WriteLine(pageContent)
	page += 1
Loop

' Close document and reader to release resources
pdfDoc.Close()
pdfReader.Close()
$vbLabelText   $csharpLabel

以上代碼中發生了很多事情。 首先,我們聲明文本提取策略,然後使用PdfExtractor類的GetTextFromPage方法來讀取文本。 此方法接受兩個參數:第一個是PDF文檔頁面,第二個是策略。 要獲得PDF文檔頁面,請使用PdfDocument的實例來調用GetPage方法並將頁面號作為參數傳遞。 輸出將作為字符串返回,然後顯示在控制台輸出屏幕上。 最後,PDFReaderPdfDocument對象被關閉。 同時,觀看使用iText7從PDF提取文本的以下代碼示例。

輸出

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 10

使用 IronPDF

就像打開PDF文件一樣只需要一行代碼,類似地,從PDF文件讀取文本也是一個單行過程。 使用PDFDocument類提供的ExtractAllText方法讀取整個PDF的內容Console.WriteLine被用來在屏幕上打印文本。 代碼如下:

// Extract all text from the PDF document
string text = pdf.ExtractAllText();

// Display the extracted text
Console.WriteLine(text);
// Extract all text from the PDF document
string text = pdf.ExtractAllText();

// Display the extracted text
Console.WriteLine(text);
' Extract all text from the PDF document
Dim text As String = pdf.ExtractAllText()

' Display the extracted text
Console.WriteLine(text)
$vbLabelText   $csharpLabel

輸出

Itext7 Read PDF in C# Alternatives (VS IronPDF) Figure 11

輸出準確無誤。 然而,要使用ExtractAllText方法,您需要擁有許可證,因為它僅在生產模式中工作。 您可以從IronPDF試用許可證頁面獲得30天的試用許可證。

比較

在比較中,這兩個庫在從PDF文檔中提取文本時均能提供100%的準確度。 在準確性方面它們是相同的。 然而,IronPDF在性能和代碼可讀性方面更有效率。

IronPDF僅使用兩行代碼即可完成iText的相同任務。 它提供即出框的文本提取方法,無需實現任何其他邏輯。 iText代碼有些複雜,您必須關閉在打開PDF文檔時創建的兩個實例。 而IronPDF在任務完成後自動清除記憶體。

摘要

在本文中,我們探討了如何使用iText庫在C#中讀取PDF文檔,然後將其與IronPDF進行比較。 這兩個庫均能提供準確的結果並提供多種PDF操作方法以便操作。 您可以使用這兩個庫創建、編輯和讀取PDF文件中的數據。

iText是開源的並且免費使用但有一些限制。 它可以取得商業使用許可。 IronPDF is also free to use and can be licensed for commercial activities with a 30-day free trial available.

下載IronPDF並嘗試一下

請注意iText 7 是其各自擁有者的註冊商標。 本網站與 iText 7 無關,未受其認可或贊助。所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

什麼是 IronPDF,它如何與 iText 7 比較?

IronPDF 是一個 .NET 庫,用於從 HTML、CSS 和 JavaScript 生成和操作 PDF 文件。與 iText 7 相比,IronPDF 更強調速度、易用性和準確性,只需較少的代碼行即可完成 PDF 任務。

怎樣在 C# 中將 HTML 轉換為 PDF?

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

在 C# 專案中安裝 IronPDF 的步驟是什麼?

要在 C# 專案中安裝 IronPDF,請在 Visual Studio 中打開 NuGet 套件管理器,搜索 IronPDF,為您的專案選擇它,然後點擊安裝。在 C# 文件的頂部包含 using IronPdf;

如何使用 IronPDF 從 PDF 中提取文本?

要使用 IronPDF 從 PDF 中提取文本,請利用 PdfDocument 類的 FromFile 方法加載 PDF,然後使用 ExtractAllText 方法檢索文本。

使用 IronPDF 的一些故障排除提示是什麼?

確保通過 NuGet 正確安裝 IronPDF,並且在您的 C# 文件中包含了正確的命名空間。驗證文件路徑,並確保在將 HTML 轉換為 PDF 時 HTML內容格式良好。

IronPDF 能處理 PDF 表單和註釋嗎?

是的,IronPDF 支持填寫表單和添加註釋等功能,使您能夠創建互動性和動態的 PDF 文件。

IronPDF 是免費使用的嗎?

IronPDF 提供免費版本,具有有限功能,其商業版本提供 30 天免費試用,提供完整的功能範圍。

使用 iText 7 進行 PDF 操作的局限性是什麼?

雖然 iText 7 是一個功能強大的 PDF 庫,但對於某些任務(如文本提取)需要額外的邏輯,這可能導致比 IronPDF 更為複雜和冗長的代碼。

Curtis Chau
技術作家

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

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