產品比較

iText7 從 PDF 中提取文本 vs IronPDF(代碼示例教程)

發佈 2023年2月2日
分享:

在本教程中,我們將學習如何從 PDF 讀取資料 (可攜式文件格式) 使用兩種不同工具在C#中處理文件的範例。

線上有許多解析函式庫/閱讀器可以從PDF中提取文字和圖片。我們將使用至今最有用且最好的兩個函式庫從PDF文件中提取信息。我們還將比較這兩個函式庫,以找出哪一個更好。

我們將比較 iText 7IronPDF. 在進一步了解之前,我們會介紹這兩個庫。

iText 7

iText 7 庫是 iTextSharp 的最新版本。它在 .NET 和 Java 應用程式中使用。它配備了一個文件引擎 (如 Adobe Acrobat Reader)高級與初級程式設計功能、事件監聽器以及PDF編輯功能。iText 7可以創建、編輯和增強PDF文檔的頁面而不會有任何錯誤。其他特點包括添加密碼、創建編碼策略和保存權限選項到PDF文檔。它還用於添加或更改內容或畫布圖像、附加PDF元素。[字典等。], 創建浮水印和書籤、更改字體大小,並簽署敏感數據。

iText 7 允許我們在 .NET 中為網絡、移動、桌面、核心或雲應用程序構建自定義 PDF 處理應用程序。

IronPDF

IronPDF 是由 Iron Software 開發的一個庫,旨在幫助 C# 和 Java 軟體工程師創建、編輯和提取 PDF 內容。它通常用於從 HTML、網頁或圖像生成 PDF,也可以用於讀取 PDF 並提取其文本。其他功能包括添加頁眉/頁腳、簽名、附件、密碼和安全問題。IronPDF 提供了多執行緒和異步功能,以實現完全性能優化。

IronPDF 具有跨平台支持,兼容 .NET 5、.NET 6 和 .NET 7、.NET Core、Standard 和 Framework。它也兼容 Windows、macOS、Linux、Docker、Azure 和 AWS。

現在,讓我們看看它們的演示。

使用 iText 7 從 PDF 文件中提取文字

我們將使用以下 PDF 文件來從 PDF 中提取文字。

從 PDF 中提取文本:iText vs IronPDF - 圖 1:PDF 文件

IronPDF

編寫以下使用iText 7提取文本的源代碼。

//assign PDF location to a string and create new StringBuilder...
string pdfPath = @"D:/TestDocument.pdf";
 var pageText = new StringBuilder();
//read PDF using new PdfDocument and new PdfReader...
 using (PdfDocument document = new PdfDocument(new PdfReader(pdfPath)))
    {
      var pageNumbers = document.GetNumberOfPages();
       for (int page = 1; page <= pageNumbers; page++)
        {
//new LocationTextExtractionStrategy creates a new text extraction renderer
    LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
     PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
     parser.ProcessPageContent(document.GetFirstPage());
     pageText.Append(strategy.GetResultantText());
         }
            Console.WriteLine(pageText.ToString());
     }
//assign PDF location to a string and create new StringBuilder...
string pdfPath = @"D:/TestDocument.pdf";
 var pageText = new StringBuilder();
//read PDF using new PdfDocument and new PdfReader...
 using (PdfDocument document = new PdfDocument(new PdfReader(pdfPath)))
    {
      var pageNumbers = document.GetNumberOfPages();
       for (int page = 1; page <= pageNumbers; page++)
        {
//new LocationTextExtractionStrategy creates a new text extraction renderer
    LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
     PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
     parser.ProcessPageContent(document.GetFirstPage());
     pageText.Append(strategy.GetResultantText());
         }
            Console.WriteLine(pageText.ToString());
     }
'assign PDF location to a string and create new StringBuilder...
Dim pdfPath As String = "D:/TestDocument.pdf"
 Dim pageText = New StringBuilder()
'read PDF using new PdfDocument and new PdfReader...
 Using document As New PdfDocument(New PdfReader(pdfPath))
	  Dim pageNumbers = document.GetNumberOfPages()
	   For page As Integer = 1 To pageNumbers
'new LocationTextExtractionStrategy creates a new text extraction renderer
	Dim strategy As New LocationTextExtractionStrategy()
	 Dim parser As New PdfCanvasProcessor(strategy)
	 parser.ProcessPageContent(document.GetFirstPage())
	 pageText.Append(strategy.GetResultantText())
	   Next page
			Console.WriteLine(pageText.ToString())
 End Using
VB   C#
Extracting Text from PDF: iText vs IronPDF - Figure 2: 提取的文本輸出

提取的文本輸出

現在,讓我們使用IronPDF從PDF中提取文本。

使用 IronPDF 從 PDF 文件中提取文本

以下源代碼示範了如何使用 IronPDF 從 PDF 中提取文本。

var pdf = PdfDocument.FromFile(@"D:/TestDocument.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
var pdf = PdfDocument.FromFile(@"D:/TestDocument.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
Dim pdf = PdfDocument.FromFile("D:/TestDocument.pdf")
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
VB   C#
Extracting Text from PDF: iText vs IronPDF - Figure 3: 使用 IronPDF 提取的文字

使用 IronPDF 提取的文字

比較

使用IronPDF,只需要兩行代碼即可從PDF中提取文本。而使用iText 7,則需要編寫大約10行代碼才能完成相同的任務。

IronPDF提供開箱即用的方便文本提取方法;但iText 7需要我們編寫自己的邏輯來完成相同的任務。

IronPDF在性能和代碼可讀性方面都很高效。

這兩個函式庫在準確性方面是相等的,因為它們都能提供100%準確的輸出。

結論

iText 7 可用於 商業用途 僅限。IronPDF 對於開發是免費的,並且還提供 免費試用商業用途如需更深入比較 IronPDF 和 iText 7,請閱讀這篇 部落格帖子.

< 上一頁
使用 IronPDF 進行產品比較
下一個 >
IronPDF 與 PDFium.NET 的比較

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

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >