在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
在本教程中,我們將學習如何從 PDF 讀取資料 (可攜式文件格式) 使用兩種不同工具在C#中處理文件的範例。
線上有許多解析函式庫/閱讀器可以從PDF中提取文字和圖片。我們將使用至今最有用且最好的兩個函式庫從PDF文件中提取信息。我們還將比較這兩個函式庫,以找出哪一個更好。
我們將比較 iText 7 和 IronPDF. 在進一步了解之前,我們會介紹這兩個庫。
iText 7 庫是 iTextSharp 的最新版本。它在 .NET 和 Java 應用程式中使用。它配備了一個文件引擎 (如 Adobe Acrobat Reader)高級與初級程式設計功能、事件監聽器以及PDF編輯功能。iText 7可以創建、編輯和增強PDF文檔的頁面而不會有任何錯誤。其他特點包括添加密碼、創建編碼策略和保存權限選項到PDF文檔。它還用於添加或更改內容或畫布圖像、附加PDF元素。[字典等。], 創建浮水印和書籤、更改字體大小,並簽署敏感數據。
iText 7 允許我們在 .NET 中為網絡、移動、桌面、核心或雲應用程序構建自定義 PDF 處理應用程序。
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。
現在,讓我們看看它們的演示。
我們將使用以下 PDF 文件來從 PDF 中提取文字。
編寫以下使用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
現在,讓我們使用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)
使用IronPDF,只需要兩行代碼即可從PDF中提取文本。而使用iText 7,則需要編寫大約10行代碼才能完成相同的任務。
IronPDF提供開箱即用的方便文本提取方法;但iText 7需要我們編寫自己的邏輯來完成相同的任務。
IronPDF在性能和代碼可讀性方面都很高效。
這兩個函式庫在準確性方面是相等的,因為它們都能提供100%準確的輸出。
iText 7 可用於 商業用途 僅限。IronPDF 對於開發是免費的,並且還提供 免費試用 為 商業用途如需更深入比較 IronPDF 和 iText 7,請閱讀這篇 部落格帖子.