如何在 OCR C# 中读取 PDF

如何使用 IronOCR 在 C# 中读取 PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

PDF是"便携式文档格式"(Portable Document Format)的缩写。它是由Adobe公司开发的一种文件格式,无论使用何种应用程序和平台创建,都能保留源文档的字体、图像、图形和布局。 PDF 文件通常用于以一致的格式共享和查看文档,而无需考虑打开它们的软件或硬件。 IronOcr 可以轻松处理各种版本的 PDF 文档。

快速入门:几秒钟内完成 PDF 文件的 OCR 识别

使用 IronOCR 快速设置 OCR,只需构建一个指向 PDF 的 OcrPdfInput,然后调用 Read 即可——无需复杂的配置。 这个单行示例展示了使用 IronOCR 从 PDF 中提取文本是多么容易。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronOCR

    PM > Install-Package IronOcr

  2. 复制并运行这段代码。

    using var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", PdfContents.TextAndImages));
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronOCR,免费试用!
    arrow pointer


阅读 PDF 示例

首先实例化 IronTesseract 类以执行 OCR。 然后,使用"using"语句创建一个OcrPdfInput对象,并将PDF文件路径传递给它。 最后,使用Read方法执行 OCR。

:path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf.cs
using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add PDF
Private pdfInput = New OcrPdfInput("Potter.pdf")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
$vbLabelText   $csharpLabel
阅读PDF文件

大多数情况下,无需指定 DPI 属性。 但是,在构建OcrPdfInput时提供较高的 DPI 值可以提高读取精度。

阅读 PDF 页面示例

在读取 PDF 文档中的特定页面时,用户可以指定要导入的页面索引号。 为此,在构造OcrPdfInput时,将页面索引列表传递给 PageIndices 参数。 请注意,页面索引采用从零开始的编号。

:path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf-pages.cs
using IronOcr;
using System.Collections.Generic;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Create page indices list
List<int> pageIndices = new List<int>() { 0, 2 };

// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf", PageIndices: pageIndices);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
Imports IronOcr
Imports System.Collections.Generic

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Create page indices list
Private pageIndices As New List(Of Integer)() From {0, 2}

' Add PDF
Private pdfInput = New OcrPdfInput("Potter.pdf", PageIndices:= pageIndices)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
$vbLabelText   $csharpLabel

指定扫描区域

通过缩小阅读范围,可以显著提高阅读效率。 为此,您可以指定需要读取的导入 PDF 的确切区域。 在下面的代码示例中,我指示 IronOcr 只专注于提取章节编号和标题。

:path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-specific-region.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Specify crop regions
Rectangle[] scanRegions = { new Rectangle(550, 100, 600, 300) };

// Add PDF
using (var pdfInput = new OcrPdfInput("Potter.pdf", ContentAreas: scanRegions))
{
    // Perform OCR
    OcrResult ocrResult = ocrTesseract.Read(pdfInput);

    // Output the result to console
    Console.WriteLine(ocrResult.Text);
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Specify crop regions
Private scanRegions() As Rectangle = { New Rectangle(550, 100, 600, 300) }

' Add PDF
Using pdfInput = New OcrPdfInput("Potter.pdf", ContentAreas:= scanRegions)
	' Perform OCR
	Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)

	' Output the result to console
	Console.WriteLine(ocrResult.Text)
End Using
$vbLabelText   $csharpLabel

OCR结果

阅读特定区域

常见问题解答

如何在 C# 中读取 PDF 文件?

您可以通过使用 IronOCR 在 C# 中读取 PDF 文件。首先实例化 IronTesseract 类,然后使用 'using' 语句创建一个带有文件路径的 OcrPdfInput 对象。最后,应用 Read 方法对文档进行 OCR。

执行 PDF 特定页面上的 OCR 需要哪些步骤?

要使用 IronOCR 在 PDF 的特定页面上执行 OCR,请在构造 OcrPdfInput 时将页面索引列表传递给 PageIndices 参数。在 IronOCR 中,页面索引是从零开始的,因此第一页的索引为 0。

如何提高 PDF 上的 OCR 准确性?

您可以在构造 OcrPdfInput 时指定更高的 DPI,以提高 IronOCR 中 PDF 的 OCR 准确性。虽然通常没有必要,但更高的 DPI 可以提高阅读精度。

是否可以选择 PDF 的特定区域进行 OCR 处理?

是的,使用 IronOCR,您可以使用 SelectRegion 方法选择 PDF 的特定区域进行 OCR 处理。这使您能够专注于从定义的区域提取内容,从而提高效率。

在读取 PDF 页面时,零基编号的重要性是什么?

在 IronOCR 中,零基编号用于指定读取 PDF 页面的页面索引。这意味着第一页的索引为 0,这有助于准确指定要处理的页面。

在对 PDF 执行 OCR 时,我需要手动管理资源吗?

使用 IronOCR 时,建议在使用 OcrInput 对象时使用 'using' 语句。这可确保在 OCR 过程完成后正确释放资源。

如何开始使用 IronOCR 进行 PDF 阅读?

要开始使用 IronOCR 阅读 PDF,请从 NuGet 下载 C# 库,准备您的 PDF,使用文件路径构造 OcrPdfInput 对象,并使用 Read 方法进行 OCR 处理。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布