产品比较

itext7从PDF中提取文本与IronPDF(代码示例教程)

发布 2023年二月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 支持跨平台,兼容 .NET 5、.NET 6 和 .NET 7、.NET Core、Standard 和 Framework。 它还兼容 Windows、macOS、Linux、Docker、Azure 和 AWS。

现在,让我们来看看它们的演示。

使用 iText 7 从 PDF 文件中提取文本

我们将使用以下 PDF 文件来提取 PDF 中的文本。

从 PDF 中提取文本:iText 与 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.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >