如何使用IronOCR在C#中提取读取结果
IronOCR的Read方法返回一个OcrResult对象,包含提取的文本以及详细的元数据,其中包括精确的坐标、尺寸、文本方向和每个检测元素的分层结构(段落、行、词、字符)。
OCR 结果包含有关检测到的段落、行、单词和单个字符的全面信息。
对于每个元素,它提供文本内容、精确的X和Y坐标、尺寸(宽度和高度)、文本方向(从左到右或从上到下),以及在CropRectangle对象中的位置。
使用IronTesseract的Read方法来对图像进行OCR,并使用Words集合提取第一个词的文本。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;Dim wordText As String = New IronTesseract().Read("file.jpg").Words(0).TextDim wordText As String = New IronTesseract().Read("file.jpg").Words(0).Text -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载用于访问读取结果的 C# 库
- 准备目标图像和 PDF 文档
- 使用
Read方法对导入的文档执行 OCR 操作。 - 访问结果的
X、Y、width、height和text direction - 检查检测到的段落、行、单词和字符比较
我可以从 OCR 结果中提取哪些数据?
结果值不仅包含提取的文本,还包含 IronOCR 在 PDF 和图像文件中发现的页面、段落、行、单词、字符和条形码等信息。 您可以使用Read方法从返回的OcrResult对象中访问此信息。
IronOCR 的综合结果系统构建在强大的Tesseract 5 引擎之上,为开发人员提供了超越简单文本识别的结构化数据提取能力。 无论是处理扫描文档、照片还是截图,OcrResult类都能让您对提取的数据进行精细控制。
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Output information to console
Console.WriteLine($"Text: {paragraphs[0].Text}");
Console.WriteLine($"X: {paragraphs[0].X}");
Console.WriteLine($"Y: {paragraphs[0].Y}");
Console.WriteLine($"Width: {paragraphs[0].Width}");
Console.WriteLine($"Height: {paragraphs[0].Height}");
Console.WriteLine($"Text direction: {paragraphs[0].TextDirection}");using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Output information to console
Console.WriteLine($"Text: {paragraphs[0].Text}");
Console.WriteLine($"X: {paragraphs[0].X}");
Console.WriteLine($"Y: {paragraphs[0].Y}");
Console.WriteLine($"Width: {paragraphs[0].Width}");
Console.WriteLine($"Height: {paragraphs[0].Height}");
Console.WriteLine($"Text direction: {paragraphs[0].TextDirection}");Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Private paragraphs() As Paragraph = ocrResult.Paragraphs
' Output information to console
Console.WriteLine($"Text: {paragraphs(0).Text}")
Console.WriteLine($"X: {paragraphs(0).X}")
Console.WriteLine($"Y: {paragraphs(0).Y}")
Console.WriteLine($"Width: {paragraphs(0).Width}")
Console.WriteLine($"Height: {paragraphs(0).Height}")
Console.WriteLine($"Text direction: {paragraphs(0).TextDirection}")Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Private paragraphs() As Paragraph = ocrResult.Paragraphs
' Output information to console
Console.WriteLine($"Text: {paragraphs(0).Text}")
Console.WriteLine($"X: {paragraphs(0).X}")
Console.WriteLine($"Y: {paragraphs(0).Y}")
Console.WriteLine($"Width: {paragraphs(0).Width}")
Console.WriteLine($"Height: {paragraphs(0).Height}")
Console.WriteLine($"Text direction: {paragraphs(0).TextDirection}")
如何从 OCR 结果中获取文本内容?
OcrResult对象以简单、直观的方式展示提取的文本,允许开发者直接使用或将其集成到其他应用程序组件中。 分层结构反映了自然文档的文本组织,使其可以直接处理不同粒度级别的内容。
对于需要多语言支持的应用程序,IronOCR 可以处理多语言文档,保持在所有支持的 125 种语言中相同的结构化结果格式。
以下代码示例在一个循环中打印文本以验证结果。
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
// Print the text of the current paragraph
Console.WriteLine(paragraph.Text);
// Add a blank line for better separation (optional)
Console.WriteLine();
}using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
// Print the text of the current paragraph
Console.WriteLine(paragraph.Text);
// Add a blank line for better separation (optional)
Console.WriteLine();
}Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End UsingImports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End Using输出

控制台输出显示 IronOCR 正在逐行精确提取段落文本。 该引擎可自动检测段落边界,因此非常适合处理包含多个文本块的复杂文档。
如何获取检测文本的位置坐标?
除了提取的文本,OcrResult还提供详细的位置数据。 这些空间信息对于需要保持布局保真度或从特定文档区域执行目标文本提取的应用程序至关重要。 坐标系使用从页面左上角开始的标准像素测量。
为了在基于坐标的操作中提升精度,您可以使用OCR 区域定位来专注于特定区域,或者利用计算机视觉功能自动识别文本区域。
以下代码演示了迭代每个段落并将其坐标(X 和 Y)打印到控制台。
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---");
// Use a 'for' loop to get an index for each paragraph
for (int i = 0; i < paragraphs.Length; i++)
{
// Get the current paragraph
Paragraph paragraph = paragraphs[i];
// Print the paragraph number, text, and its location
Console.WriteLine($"X: {paragraph.X}"); // X-coordinate (left edge)
Console.WriteLine($"Y: {paragraph.Y}"); // Y-coordinate (top edge)
// Add a blank line for better separation
Console.WriteLine();
}using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---");
// Use a 'for' loop to get an index for each paragraph
for (int i = 0; i < paragraphs.Length; i++)
{
// Get the current paragraph
Paragraph paragraph = paragraphs[i];
// Print the paragraph number, text, and its location
Console.WriteLine($"X: {paragraph.X}"); // X-coordinate (left edge)
Console.WriteLine($"Y: {paragraph.Y}"); // Y-coordinate (top edge)
// Add a blank line for better separation
Console.WriteLine();
}Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---")
' Use a 'for' loop to get an index for each paragraph
For i As Integer = 0 To paragraphs.Length - 1
' Get the current paragraph
Dim paragraph As Paragraph = paragraphs(i)
' Print the paragraph number, text, and its location
Console.WriteLine($"X: {paragraph.X}") ' X-coordinate (left edge)
Console.WriteLine($"Y: {paragraph.Y}") ' Y-coordinate (top edge)
' Add a blank line for better separation
Console.WriteLine()
Next
End UsingImports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---")
' Use a 'for' loop to get an index for each paragraph
For i As Integer = 0 To paragraphs.Length - 1
' Get the current paragraph
Dim paragraph As Paragraph = paragraphs(i)
' Print the paragraph number, text, and its location
Console.WriteLine($"X: {paragraph.X}") ' X-coordinate (left edge)
Console.WriteLine($"Y: {paragraph.Y}") ' Y-coordinate (top edge)
' Add a blank line for better separation
Console.WriteLine()
Next
End Using输出

输出结果显示了与三个段落相对应的三组坐标。这些坐标可用于绘制边界框、提取特定区域或维护文本元素之间的空间关系。
在 OCR 结果中还有其他哪些属性可用?
除了文本和文本坐标,IronOCR 还提供了其他信息。 对于每个文本元素(段落、行、单词和单个字符),可提供以下信息:
- 文本:作为字符串的实际文本内容。
- X:相对于页面左边缘的像素位置。
- Y:相对于页面顶边缘的像素位置。
- 宽度:以像素为单位的宽度。
- 高度:以像素为单位的高度。
- 文本方向:文本的阅读方向(从左到右或从上到下)。
- 位置:一个矩形,以像素为单位显示该文本在页面上的位置。
这些特性在实施时尤其有用:
- 文本高亮和注释系统
- 自动表单字段检测
- 文档转换中的版面保留
- 用于数据提取的空间文本分析
用于调试和可视化,请使用高亮文本功能以视觉方式验证检测区域的准确性。
段落、行、单词和字符如何比较?
IronOCR 的分层文本结构允许开发人员根据具体使用情况在适当的细节级别上工作。 了解这些元素之间的差异有助于为您的应用程序选择正确的粒度。
以下是检测到的段落、行、单词和字符的对比。
![]() Paragraph | ![]() Line |
![]() Word | ![]() Character |
每个粒度级别都有不同的目的:
- 段落: 适合文档结构分析及批量文本提取
- 行: 对保持阅读顺序和处理表格数据有用
- 单词: 适用于搜索功能和文本分析
- 字符: 完美适用于拼写检查和精确文本编辑应用
IronOCR 能读取条形码和 QR 码吗?
是的,IronOCR 可以读取条形码和 QR 码。 虽然该功能可能不如IronBarcode可靠,IronOCR提供了对常见条码类型的支持。要启用条码检测,请将Configuration.ReadBarCodes属性设置为true。 这种集成功能使 IronOCR 成为处理包含文本和 BarCode 的文档(如发票、发货标签或产品目录)的绝佳选择。
此外,可以从检测到的条码中提取有价值的信息,包括格式、值、坐标(x, y)、高度、宽度以及作为IronSoftware.Drawing.Rectangle对象的位置。 在IronDrawing中的这个Rectangle类允许在文档上进行精确定位。
有关更高级条形码读取场景,请查看我们文档中的详细条形码读取示例。
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = true;
// Add image
using OcrInput ocrInput = new OcrInput();
ocrInput.LoadPdf("sample.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
// Output information to console
foreach(var barcode in ocrResult.Barcodes)
{
Console.WriteLine("Format = " + barcode.Format);
Console.WriteLine("Value = " + barcode.Value);
Console.WriteLine("X = " + barcode.X);
Console.WriteLine("Y = " + barcode.Y);
}
Console.WriteLine(ocrResult.Text);using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = true;
// Add image
using OcrInput ocrInput = new OcrInput();
ocrInput.LoadPdf("sample.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
// Output information to console
foreach(var barcode in ocrResult.Barcodes)
{
Console.WriteLine("Format = " + barcode.Format);
Console.WriteLine("Value = " + barcode.Value);
Console.WriteLine("X = " + barcode.X);
Console.WriteLine("Y = " + barcode.Y);
}
Console.WriteLine(ocrResult.Text);Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = True
' Add image
Using ocrInput As New OcrInput()
ocrInput.LoadPdf("sample.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
' Output information to console
For Each barcode In ocrResult.Barcodes
Console.WriteLine("Format = " & barcode.Format)
Console.WriteLine("Value = " & barcode.Value)
Console.WriteLine("X = " & barcode.X)
Console.WriteLine("Y = " & barcode.Y)
Next barcode
Console.WriteLine(ocrResult.Text)
End UsingImports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = True
' Add image
Using ocrInput As New OcrInput()
ocrInput.LoadPdf("sample.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
' Output information to console
For Each barcode In ocrResult.Barcodes
Console.WriteLine("Format = " & barcode.Format)
Console.WriteLine("Value = " & barcode.Value)
Console.WriteLine("X = " & barcode.X)
Console.WriteLine("Y = " & barcode.Y)
Next barcode
Console.WriteLine(ocrResult.Text)
End Using条形码检测的输出是什么样的?
IronOCR 中的条形码检测功能与文本提取集成,提供统一的结果,包括文本内容和条形码数据。 这种双重能力对于需要提取和关联两种信息类型的自动文档处理工作流程非常有价值。

输出结果展示了 IronOCR 同时检测多种条形码类型的能力,为每个检测到的代码提供格式标识(如 QRCode 或 EAN8)、解码值和精确的坐标信息。 通过这些全面的数据,开发人员可以构建复杂的文档处理应用程序,高效处理混合内容类型。
常见问题
IronOCR中的OcrResult对象是什么?
IronOCR中的OcrResult对象包含提取的文本和详细的元数据,包括精确的坐标、尺寸、文本方向,以及检测元素(如段落、行、单词和字符)的层次结构。
如何使用IronOCR提取第一个单词的文本?
您可以使用IronTesseract的Read方法对图像进行OCR,并使用Words集合提取第一个单词的文本。例如,new IronTesseract().Read("file.jpg").Words[0].Text。
使用IronOCR,我可以从OCR结果中提取哪些数据?
IronOCR允许从PDF和图像文档中提取文本、页面、段落、行、单词、字符和条形码,以及元数据,如位置坐标和文本方向。
IronOCR 能处理多种语言吗?
是的,IronOCR支持多语言文档,并在所有125种支持的语言中维持结构化的结果格式,非常适合需要多语言支持的应用程序。
如何在IronOCR中获取检测文本的位置坐标?
OcrResult对象提供详细的位置数据,包括每个检测文本元素的X和Y坐标、宽度和高度,使用像素从页面左上角测量的坐标系统。
使用IronOCR,OCR结果中有哪些属性可用?
对于每个文本元素,IronOCR提供文本、X和Y坐标、宽度、高度、文本方向和位置。这些属性便利了诸如文本高亮、表单字段检测和文档布局保留等应用程序。
IronOCR 能读取条形码和二维码吗?
是的,IronOCR可以通过在配置中启用条形码检测来读取常见的条形码类型,包括QR码。此集成允许对包含文本和条形码的文档进行全面处理。
IronOCR如何帮助进行层次文本分析?
IronOCR的层次文本结构允许开发人员在不同的粒度级别上工作,例如段落、行、单词和字符,使其适用于各种应用程序,如文档结构分析和文本搜索功能。
IronOCR在坐标基于操作方面提供了哪些高级功能?
IronOCR提供OCR区域定位以进行集中的文本提取,并具备计算机视觉能力自动识别文本区域,提高坐标基于应用程序的精度。
IronOCR中的条形码检测输出是什么样的?
IronOCR中的条形码检测输出包括格式识别、解码值和精确坐标,结合文本提取以有效处理混合内容文档。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。



