使用 IronOCR 在 C# 中读取扫描文档
IronOCR 使 C# 开发人员能够使用 OCR 技术从扫描的 PDF 和图像中提取文本,只需几行代码就能将不可搜索的基于图像的文档转换为可搜索、可访问的内容。
许多 PDF 文件包含无法搜索的、基于图像的文本。 IronOCR 将其转换为可搜索的内容,使查找特定信息变得更加容易,并增强了文档的可访问性,尤其是对有视觉障碍的个人而言。
自动提取无需手动复制或重新创建文本和图像,从而确保准确性和效率。 这对研究、法律文件和内容创建特别有用,因为在这些领域中,重复使用 PDF 的特定部分很常见。
企业可以从PDF中提取关键数据用于分析或系统集成,从而简化工作流程。 设计师和营销人员还可以提取图像,以便在各种项目中进行增强和重复使用。
在本教程中,我们将探讨OcrPdfInput方法,涵盖可用的选项和参数,以展示IronOCR如何简化各种应用的PDF文本和图像提取。
要使用此功能,您还必须安装IronOcr.Extensions.AdvancedScan包。
只需一行代码即可在几秒钟内开始使用 - 使用IronOCR的OcrInput.LoadPdf或LoadImage加载您的扫描PDF或图像,并通过ReadDocument立即提取文本。 非常适合希望快速启动并运行 OCR 功能的开发人员。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
var input = new IronOcr.OcrInput(); input.LoadPdf("scanned.pdf"); var text = new IronOcr.IronTesseract().ReadDocument(input).Text;Dim input = New IronOcr.OcrInput() input.LoadPdf("scanned.pdf") Dim text = New IronOcr.IronTesseract().ReadDocument(input).TextDim input = New IronOcr.OcrInput() input.LoadPdf("scanned.pdf") Dim text = New IronOcr.IronTesseract().ReadDocument(input).Text -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载用于读取扫描文档的C#库
- 导入扫描文档以进行处理
- 对于图像,请使用
LoadImage方法;对于扫描的 PDF 文件,请使用LoadPdf方法。 - 使用
ReadDocument方法提取文本 - 根据需要保存或导出提取的文本以供进一步使用
我如何从扫描文档中提取文本?
要从文档中的所有图像中提取文本,请使用ReadDocument方法。 此方法处理文档并返回一个包含提取文本的对象,可以通过Text属性访问。 下面的示例演示了如何使用该方法处理示例TIFF文件。
IronOCR 支持多种文件格式的扫描。 对于图像,您可以处理JPG、PNG、GIF、TIFF和BMP格式,而PDF支持包括单页和多页文档。 该库使用先进的Tesseract 5技术以确保所有支持格式的高精度。
输入文档是什么样的?

我如何实现OCR代码?
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
// Configure OCR engine
using var input = new OcrInput();
input.LoadImage("potter.tiff");
// Perform OCR
OcrResult result = ocr.ReadDocument(input);
Console.WriteLine(result.Text);using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
// Configure OCR engine
using var input = new OcrInput();
input.LoadImage("potter.tiff");
// Perform OCR
OcrResult result = ocr.ReadDocument(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
' Instantiate OCR engine
Dim ocr As New IronTesseract()
' Configure OCR engine
Using input As New OcrInput()
input.LoadImage("potter.tiff")
' Perform OCR
Dim result As OcrResult = ocr.ReadDocument(input)
Console.WriteLine(result.Text)
End UsingImports IronOcr
Imports System
' Instantiate OCR engine
Dim ocr As New IronTesseract()
' Configure OCR engine
Using input As New OcrInput()
input.LoadImage("potter.tiff")
' Perform OCR
Dim result As OcrResult = ocr.ReadDocument(input)
Console.WriteLine(result.Text)
End Using我可以从OCR处理获得什么结果?

如果您需要对PDF文件进行OCR,只需将LoadImage方法替换为LoadPdf即可。 这样,IronOCR 就可以用同样的方法处理和提取扫描 PDF 中的文本。
高级文档处理选项
在处理扫描文档时,您通常需要对 OCR 过程进行更多控制。 IronOCR 提供多种高级功能,可增强文本提取结果。
处理多页文档
对于有多个页面的文档,IronOCR 可以高效地进行批处理:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf");
// Process all pages
OcrResult result = ocr.ReadDocument(input);
// Access individual page results
foreach (var page in result.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words");
}using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf");
// Process all pages
OcrResult result = ocr.ReadDocument(input);
// Access individual page results
foreach (var page in result.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words");
}Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf")
' Process all pages
Dim result As OcrResult = ocr.ReadDocument(input)
' Access individual page results
For Each page In result.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words")
Next
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf")
' Process all pages
Dim result As OcrResult = ocr.ReadDocument(input)
' Access individual page results
For Each page In result.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words")
Next
End Using优化OCR性能
扫描文件的质量直接影响 OCR 的准确性。 IronOCR包括内置的图像优化过滤器以增强文本识别:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load and enhance image quality
input.LoadImage("low-quality-scan.jpg");
input.Deskew(); // Correct image skew
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
OcrResult result = ocr.ReadDocument(input);using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load and enhance image quality
input.LoadImage("low-quality-scan.jpg");
input.Deskew(); // Correct image skew
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
OcrResult result = ocr.ReadDocument(input);Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load and enhance image quality
input.LoadImage("low-quality-scan.jpg")
input.Deskew() ' Correct image skew
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
Dim result As OcrResult = ocr.ReadDocument(input)
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load and enhance image quality
input.LoadImage("low-quality-scan.jpg")
input.Deskew() ' Correct image skew
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
Dim result As OcrResult = ocr.ReadDocument(input)
End Using创建可搜索的PDF
处理扫描文档时最有价值的功能之一是能够创建可搜索的PDF。 这既保持了原始文档的外观,又增加了文字层:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
// Process and save as searchable PDF
OcrResult result = ocr.ReadDocument(input);
result.SaveAsSearchablePdf("searchable-output.pdf");using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
// Process and save as searchable PDF
OcrResult result = ocr.ReadDocument(input);
result.SaveAsSearchablePdf("searchable-output.pdf");Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
' Process and save as searchable PDF
Dim result As OcrResult = ocr.ReadDocument(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
' Process and save as searchable PDF
Dim result As OcrResult = ocr.ReadDocument(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End Using处理不同类型的文档
IronOCR 擅长处理商业环境中常见的各种文档类型。 无论是处理发票、合同还是历史文档,该库都提供了从不同来源提取数据的专业功能。
处理旧版文件
许多组织都有旧格式的扫描文件档案。 IronOCR高效处理这些文件,包括支持在文档管理系统中常用的多页TIFF文件。
语言支持
虽然本示例侧重于英语文本,IronOCR支持超过125种国际语言。 因此,它非常适合处理多语言文档或非英语文档。
文件扫描的最佳实践
在处理扫描文件时达到最佳效果:
- 扫描质量:使用最低300 DPI的分辨率以获得最佳效果
- 文件格式:TIFF和PNG格式比JPEG更好地保持文本文件的质量
- 预处理:根据您的文件状况应用适当的过滤器
- 性能:对于大批量,考虑使用多线程功能
故障排除常见问题
在处理扫描文件时,您可能会遇到各种挑战。 以下是常见问题的解决方案:
- 质量差的扫描:在OCR处理前应用增强过滤器
- 倾斜的文档:使用
Deskew()方法校正方向 - 混合内容:如果文档包含文本和非文本元素,请处理特定区域
有关更详细的指导,请探索我们的综合C# OCR教程或查看简单的OCR实例以快速入门。
下一步
现在您了解了如何从扫描文档中提取文本,您可以探索更高级的功能,比如使任何PDF可搜索或处理用于Web应用程序的PDF流。 IronOCR 的灵活性使其适用于从简单的文档数字化到复杂的企业文档处理工作流程等各种情况。
常见问题
如何使用IronOCR从扫描PDF中提取文本?
使用IronOCR,您可以通过加载扫描的PDF(例如使用OcrInput.LoadPdf方法),然后调用ReadDocument()方法来提取文本。这使您可以将无法搜索的PDF转换为可读取文本的格式。
IronOCR可以处理哪些图像格式?
IronOCR支持多种图像格式,如JPG、PNG、GIF、TIFF和BMP,从而可以从各种文档类型中提取文本。
IronOCR中的`ReadDocument`方法用于什么?
ReadDocument方法处理加载的文档,并从其中的所有图像中提取文本,并通过结果对象的Text属性访问这些文本。
IronOCR 能处理多页文档吗?
是的,IronOCR可以有效地处理多页文档,如PDF,方法是用LoadPdf方法加载它们并从每一页提取文本。
如何提升扫描图像的质量以获得更好的OCR结果?
IronOCR包含图像优化过滤器,例如Deskew、DeNoise和Binarize,可以通过纠正图像倾斜、消除背景噪声并将图像转换为黑白来提高OCR准确性。
IronOCR是否支持从不同语言的文档中提取文本?
IronOCR支持超过125种语言,使其能够高效处理多语言或非英语文档。
IronOCR可用于创建可搜索的PDF提供什么支持?
借助IronOCR,您可以通过处理文档并保留文本层来将扫描的文档转换为可搜索的PDF,从而保持其原始外观并增加搜索功能。
IronOCR中需要哪个包用于高级OCR功能?
要访问IronOCR中的高级OCR功能,您需要安装IronOcr.Extensions.AdvancedScan包,该包增加了额外的文档处理功能。
扫描文档进行OCR处理的最佳实践是什么?
为了获得最佳OCR结果,请使用至少300 DPI的分辨率,将文档保存为TIFF或PNG格式,如果需要,应用预处理过滤器,并利用多线程处理大型批次。
IronOCR如何处理质量不佳的扫描件?
IronOCR可以通过使用DeNoise等增强过滤器在处理前清理图像和使用Deskew纠正任何方向问题来改善质量不佳扫描件的OCR结果。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。