如何使用IronOCR在C#中读取PDF
IronOCR 使您只需一行代码就能用 C# 从 PDF 文件中提取文本,支持所有 PDF 版本,并通过其基于 Tesseract 的引擎提供准确的 OCR 结果。
PDF 是 "便携式文档格式 "的缩写。它是由 Adobe 公司开发的一种文件格式,可以保留任何源文件的字体、图像、图形和布局,而与创建这些文件时使用的应用程序和平台无关。 PDF 文件通常用于以一致的格式共享和查看文档,而无需考虑打开它们的软件或硬件。 IronOCR 可处理各种版本的 PDF 文档,从较早的 PDF 1.0 规范到最新的 PDF 2.0 标准。
快速入门:在几秒内OCR一个PDF文件通过构造一个指向您的PDF的OcrPdfInput,然后调用Read,使用IronOCR快速配置OCR。 本例演示了使用 IronOCR 从 PDF 中提取文本。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", OcrContent: IronOcr.PdfContents.TextAndImages));Dim result = New IronOcr.IronTesseract().Read(New IronOcr.OcrPdfInput("document.pdf", OcrContent:=IronOcr.PdfContents.TextAndImages))Dim result = New IronOcr.IronTesseract().Read(New IronOcr.OcrPdfInput("document.pdf", OcrContent:=IronOcr.PdfContents.TextAndImages)) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载用于读取PDF的C#库
- 准备用于读取的 PDF 文档
- 使用 PDF 文件路径构建OcrPdfInput对象
- 使用
Read方法对导入的PDF文件执行OCR识别。 - 通过提供页面索引列表读取特定页面
如何读取整个PDF文件?
首先实例化IronTesseract类以执行OCR。 然后,利用一个 'using' 语句创建一个OcrPdfInput对象,将PDF文件路径传递给它。 最后,使用Read方法执行OCR。 此方法适用于扫描的PDF(基于图像)和可搜索的PDF(基于文本),适用于从各种PDF类型中提取文本。
/* :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);
// Access the extracted text
string extractedText = ocrResult.Text;
System.Console.WriteLine(extractedText);/* :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);
// Access the extracted text
string extractedText = ocrResult.Text;
System.Console.WriteLine(extractedText);Imports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add PDF
Using pdfInput As New OcrPdfInput("Potter.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Access the extracted text
Dim extractedText As String = ocrResult.Text
System.Console.WriteLine(extractedText)
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add PDF
Using pdfInput As New OcrPdfInput("Potter.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Access the extracted text
Dim extractedText As String = ocrResult.Text
System.Console.WriteLine(extractedText)
End Using
在大多数情况下,无需指定DPI属性。 然而,在构造OcrPdfInput时提供一个高DPI数字可以提高读取准确性。 默认 DPI 设置通常足以满足大多数标准 PDF 文档的要求,但专业文档可能需要进行调整。
何时应该调整DPI设置?
在处理低分辨率扫描文件或包含小文本的 PDF 文件时,DPI(每英寸点数)设置至关重要。 为获得最佳效果,请考虑在以下情况下调整DPI设置:
- 处理低于 200 DPI 的扫描文件
- 处理历史或档案 PDF
- 处理复杂布局或小字体
- 遇到默认设置的准确性问题
对于大多数 OCR 操作,建议使用 300 DPI,而对于文本非常小或细节非常复杂的文档,可能需要使用 600 DPI。
IronOCR除了支持PDF,还支持什么文件格式?
IronOCR 可全面支持 PDF 以外的多种文件格式。 您可以处理各种格式的图像,包括:
- 标准照片的 JPEG/JPG 格式
- PNG 用于透明图像
- 用于多页文档的 TIFF 文件
- 用于未压缩图像的 BMP
- 简单图形的 GIF
此外,IronOCR可以直接从内存中处理PDF流,适用于网络应用和云服务。
处理PDF内容类型
在处理 PDF 时,您可以通过指定内容类型来优化性能。 PdfContents枚举允许您定位特定内容:
// For PDFs that contain text (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", OcrContent: PdfContents.TextAndImages);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", OcrContent: PdfContents.OnlyImages);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", OcrContent: PdfContents.TextAndImages);// For PDFs that contain text (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", OcrContent: PdfContents.TextAndImages);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", OcrContent: PdfContents.OnlyImages);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", OcrContent: PdfContents.TextAndImages);' For PDFs that contain text (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", OcrContent:=PdfContents.TextAndImages)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", OcrContent:=PdfContents.OnlyImages)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", OcrContent:=PdfContents.TextAndImages)' For PDFs that contain text (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", OcrContent:=PdfContents.TextAndImages)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", OcrContent:=PdfContents.OnlyImages)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", OcrContent:=PdfContents.TextAndImages)如何读取PDF中的特定页面?
从 PDF 文档中读取特定页面时,请指定导入的页面索引号。 为此,请在构造OcrPdfInput时将页面索引列表传递给PageIndices参数。 请注意,页面索引采用从零开始的编号。 在处理只有某些页面包含相关信息的大型文档时,该功能尤其有用。
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);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)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)为什么页码从零开始?
零基索引是 C# 和大多数编程语言的标准约定。 这意味着第一页是索引 0,第二页是索引 1,以此类推。 这种与数组索引的一致性使开发人员更容易以编程方式处理页面集合。 从人类可读的页码(1、2、3......)转换为索引时,只需从页码中减去 1 即可。
如何读取非连续页面?
使用 IronOCR 可以直接阅读非连续页面。 只需将所需的页面索引添加到您的列表中,顺序不限。 例如:
// Read pages 1, 3, 5, and 10 (using zero-based indices)
List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };
// Or use LINQ for range-based selection
var evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();// Read pages 1, 3, 5, and 10 (using zero-based indices)
List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };
// Or use LINQ for range-based selection
var evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();Imports System.Collections.Generic
Imports System.Linq
' Read pages 1, 3, 5, and 10 (using zero-based indices)
Dim pageIndices As New List(Of Integer)() From {0, 2, 4, 9}
' Or use LINQ for range-based selection
Dim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod 2 = 0).ToList()Imports System.Collections.Generic
Imports System.Linq
' Read pages 1, 3, 5, and 10 (using zero-based indices)
Dim pageIndices As New List(Of Integer)() From {0, 2, 4, 9}
' Or use LINQ for range-based selection
Dim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod 2 = 0).ToList()OCR 引擎将只处理指定的页面,从而显著提高大型文档的性能。
如果我指定了无效的页码会怎样?
如果您指定的页面索引超过了文档的页数,IronOCR 将抛出异常。 在处理之前实施错误处理或验证页面计数。 您可以在执行 OCR 之前检查 PDF 的总页数,以确保您的索引有效。
如何对PDF的特定区域执行OCR?
通过缩小阅读范围,可以显著提高阅读效率。 为此,请指定导入 PDF 中需要阅读的精确区域。 在下面的代码示例中,IronOCR 只专注于提取章节编号和标题。 此技术类似于为图像定义OCR区域,可提高速度和准确性。
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);
}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 UsingImports 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如何确定正确的矩形坐标?

找到正确的坐标需要了解 PDF 的坐标系。 Rectangle构造函数需要四个参数:X(水平位置),Y(垂直位置),Width和Height。 所有测量值均以像素为单位。 带有标尺功能的 PDF 查看器或调试实用程序等工具可以帮助确定准确的坐标。 此外,还可以通过小幅调整反复试验来完善您的选择区域。
为更精确地定义区域,您可以利用突出显示文本进行调试功能来可视化正在处理的区域。
我能否在一次操作中指定多个区域?
是的,IronOCR 在一次 OCR 操作中支持多个区域。 只需将多个Rectangle对象添加到您的数组中:
Rectangle[] scanRegions = {
new Rectangle(50, 50, 200, 100), // Header region
new Rectangle(50, 200, 500, 300), // Main content region
new Rectangle(50, 550, 200, 50) // Footer region
};Rectangle[] scanRegions = {
new Rectangle(50, 50, 200, 100), // Header region
new Rectangle(50, 200, 500, 300), // Main content region
new Rectangle(50, 550, 200, 50) // Footer region
};Imports System.Drawing
Dim scanRegions As Rectangle() = {
New Rectangle(50, 50, 200, 100), ' Header region
New Rectangle(50, 200, 500, 300), ' Main content region
New Rectangle(50, 550, 200, 50) ' Footer region
}Imports System.Drawing
Dim scanRegions As Rectangle() = {
New Rectangle(50, 50, 200, 100), ' Header region
New Rectangle(50, 200, 500, 300), ' Main content region
New Rectangle(50, 550, 200, 50) ' Footer region
}每个区域将分别处理,处理结果将按指定顺序合并。
为何使用区域特定OCR而不是全页面?
针对特定区域的 OCR 具有以下几个优势:
- 性能:处理较小区域的速度明显更快
- 准确性:聚焦于特定区域可减少无关内容带来的噪声
- 结构:更可靠地从表单和表格中提取数据
- 成本效率:较少的处理时间意味着较低的计算成本
在处理发票、表单或报告等数据出现在可预测位置的结构化文档时,这种方法尤为重要。 对于复杂的文档结构,请探索在文档中读取表格以获取专门的表格提取技术。
有哪些高级PDF OCR功能可用?
IronOCR 为 PDF 处理提供了基本文本提取之外的其他功能。 您可以从扫描文档中创建可搜索的PDF,在保留原始布局的同时添加用于搜索和复制的文本层。 该库还支持多线程处理大量PDF集合以加快处理速度。
对于希望在其.NET应用程序中使用OCR的开发人员,探索简单的OCR示例提供了了解IronOCR功能和最佳实践的可靠基础。
处理复杂的PDF场景
在处理具有挑战性的 PDF 文档时,IronOCR 提供了多项高级功能:
这些功能使 IronOCR 成为满足企业级 PDF 处理要求的全面解决方案。
常见问题
如何使用IronOCR在C#中从PDF中提取文本?
要使用IronOCR从PDF中提取文本,您需要实例化IronTesseract类,并使用OcrPdfInput指定您的PDF文件。然后,调用Read方法执行OCR并访问提取的文本。
使用IronOCR处理扫描PDF的好处是什么?
IronOCR通过利用其基于Tesseract的引擎为扫描的PDF提供准确的OCR结果,支持图像和文本两种类型的PDF。
IronOCR能够处理不同版本的PDF吗?
是的,IronOCR支持从较旧的PDF 1.0规范到最新的PDF 2.0标准的广泛PDF版本,确保可靠的兼容性。
调整IronOCR中的DPI设置如何影响文本提取?
调整IronOCR中的DPI设置可以提高OCR的准确性,特别是对于低分辨率文件、复杂布局或小字体,推荐的设置为300或600 DPI,以适应复杂的文档。
除了PDF之外,IronOCR还能处理哪些文件格式?
除了内存中的PDF流外,IronOCR还能处理多种图像格式,包括JPEG、PNG、TIFF、BMP和GIF。
如何使用IronOCR读取PDF中的特定页面?
要读取PDF中的特定页面,请在OcrPdfInput对象中使用PageIndices参数指定页面索引号。这可以提高目标页面处理的效率。
使用特定区域OCR比使用全页OCR有什么优势?
特定区域OCR通过关注相关区域来提高性能,从而提高准确性并减少无关内容带来的噪音。它特别适用于表格和结构化文档。
IronOCR为PDF提供了哪些高级OCR功能?
IronOCR提供了诸如创建可搜索PDF、多线程快速处理、图像预处理以及多语言支持等功能,以满足复杂文档处理需求。
如何处理非法页面号在IronOCR中?
如果在IronOCR中指定了非法页面号,它将抛出一个异常。建议在处理之前实现错误处理或验证页面数量。
IronOCR能否处理来自PDF的非连续页面?
是的,IronOCR可以通过在列表中指定所需的页面索引来处理非连续页面,从而允许以任意顺序选择性地处理页面。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。