IronOCR 操作指南 数据输出 如何使用 IronOCR 在 C# 中提取读取结果 Chaknith Bin 已更新:十一月 18, 2025 下载 IronOCR NuGet 下载 DLL 下载 Windows 安装程序 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English 读取或 OCR 结果包含有关检测到的段落、行、单词和单个字符的大量信息。 对于这些要素中的每一个,结果都提供了一套全面的详细信息。 它为每个元素提供文本内容、精确的 X 和 Y 坐标、尺寸(宽度和高度)、文本方向(从左到右或从上到下)以及在CropRectangle对象中的位置。 快速入门:从检测到的第一个单词中检索单词文本 只需几秒钟即可上手:使用 IronTesseract 的 Read 方法对图像进行 OCR 识别,并使用 Words 集合提取第一个单词的文本。 非常适合快速安装和简单的提取任务。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronOCR PM > Install-Package IronOcr 复制并运行这段代码。 string wordText = new IronTesseract().Read("file.jpg").Words[0].Text; 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronOCR,免费试用! 免费试用30天 最小工作流程(5 个步骤) 下载用于访问读取结果的 C# 库 准备目标图像和 PDF 文档 使用Read方法对导入的文档执行 OCR 操作。 访问结果的 X、Y、宽度、高度和文本方向 检查检测到的段落、行、单词和字符比较 数据输出 结果值不仅包含提取的文本,还提供有关 IronOcr 在 PDF 和图像文档中发现的页面、段落、行、单词、字符和条形码的信息。 您可以使用Read方法从返回的 OcrResult 对象中访问此信息。 :path=/static-assets/ocr/content-code-examples/how-to/read-results-output-information.cs 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}") $vbLabelText $csharpLabel OCR结果中的文本 OcrResult对象以简单、直观的方式呈现提取的文本,允许开发人员直接使用它或将其集成到应用程序的其他部分中。 让我们来看一个循环打印文本的代码示例,以验证结果。 :path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs 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(); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 输出 以下是控制台输出的内容。 如您所见,IronOCR 能够逐行精确地提取段落文本。 OCR结果中的文本位置 除了提取的文本外, OcrResult还提供详细的位置数据。 以下代码演示了如何遍历每个段落并将其坐标(X 和 Y)打印到控制台。 :path=/static-assets/ocr/content-code-examples/how-to/read-results-output-coordinates.cs 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(); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 输出 从输出结果可以看出,有三组坐标分别对应于三个段落。 OCR结果中的其他属性 除了文本和文本坐标外,IronOCR 还提供其他信息。 对于文本的每个部分,例如段落、行、单词和单个字符,我们提供以下信息: 文本:实际的文本字符串。 X:从页面左边缘到该点的像素位置。 Y:从页面顶部边缘算起的像素位置。 宽度:以像素为单位的宽度。 高度:以像素为单位的高度。 文本方向:文本的阅读方向,例如"从左到右"或"从上到下"。 位置:一个矩形,以像素为单位显示此文本在页面上的位置。 段落、行、词和字符比较 下面对比的是检测到的段落、行、单词和字符。 段落 线 Word 特点 条形码和二维码 没错! IronOcr可以读取条形码和二维码。 虽然IronOcr的功能可能不如IronBarcode强大,但它确实支持常见的条形码类型。要启用条形码检测,请将Configuration.ReadBarCodes属性设置为true。 此外,还可以从检测到的条形码中提取有价值的信息,包括其格式、值、坐标(x,y)、高度、宽度和位置(Iron Software.Drawing.Rectangle 对象)。 IronDrawing中的Rectangle类允许在文档上进行精确定位。 :path=/static-assets/ocr/content-code-examples/how-to/read-results-barcodes.cs 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 Using $vbLabelText $csharpLabel 输出 常见问题解答 如何使用 C# 从图像和 PDF 中提取文本元素? 您可以使用 IronOCR 通过利用其 `Read` 方法从图像和 PDF 中提取文本元素,执行光学字符识别 (OCR) 以获取关于段落、行、单词和字符的详细信息,包括它们的文本内容、坐标和尺寸。 开始使用 .NET C# 中的 OCR 的过程是什么? 要开始使用 .NET C# 中的 OCR,从 NuGet 下载 IronOCR 库,准备您的图像或 PDF 文档,并使用 `Read` 方法获取包含详细信息的 `OcrResult` 对象,其中包含提取的文本和文档结构的信息。 IronOCR 能否检测并提取条码信息? 是的,IronOCR 可以通过将 `Configuration.ReadBarCodes` 属性设置为 true 来检测并提取条码信息,这样您就可以检索条码的格式、值及其在文档中的位置等数据。 IronOCR 可以检测到哪些类型的文档元素? IronOCR 可以检测到各种文档元素,包括页、段落、行、单词和单个字符,还可以检测条码和二维码,提供文档结构的全面分析。 我如何配置 IronOCR 以读取不同方向的文本? IronOCR 可以识别多种方向的文本,例如从左到右或从上到下,通过分析 `OcrResult` 对象中的方向属性。 IronOCR 中的 `CropRectangle` 对象是什么? `CropRectangle` 对象在 IronOCR 中定义了页面上文本元素的位置和大小,以坐标和尺寸表示,帮助准确识别和提取文本。 我如何使用 IronOCR 的 `Read` 方法来分析文档? 要使用 IronOCR 的 `Read` 方法,创建一个 IronOCR 引擎实例,加载目标文档,并执行 `Read` 方法以获取 OCR 结果,可以用于访问文本数据和文档属性。 IronOCR 如何处理二维码的检测? IronOCR 通过 `Configuration.ReadBarCodes` 设置启用条码读取来处理二维码检测,允许它提取包括格式、值及位置在内的二维码数据。 `OcrResult` 在文本提取中扮演什么角色? `OcrResult` 对象在文本提取中起到了关键作用,它保存了提取的文本及伴随的详细信息,如文本元素的位置、尺寸和方向,以及条码信息。 我如何确保 IronOCR 的文本提取准确? 要确保 IronOCR 的文本提取准确,请务必提供高质量的输入文档,并正确配置如 `Configuration.ReadBarCodes` 这样的设置以进行条码检测,从而优化 OCR 性能。 Chaknith Bin 立即与工程团队聊天 软件工程师 Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。 准备开始了吗? Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:5,167,857 查看许可证