用 IronOCR 在 C# 中将文本高亮为图像。

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

IronOCR 的HighlightTextAndSaveAsImages方法通过在检测到的文本(字符、单词、行或段落)周围绘制边框来实现 OCR 结果的可视化,并将其保存为诊断图像,从而使开发人员能够验证 OCR 的准确性并调试识别问题。

可视化 OCR 结果包括在引擎检测到的图像中的特定文本元素周围呈现边界框。 该流程可在单个字符、单词、行或段落的准确位置上叠加明显的高亮标记,提供清晰的识别内容地图。

这种可视化反馈对于调试和验证 OCR 输出的准确性至关重要,它可以显示软件已经识别出的内容和出错的地方。 在处理复杂文档或排除识别问题时,可视化高亮显示成为必不可少的诊断工具。

本文通过 HighlightTextAndSaveAsImages 方法演示了 IronOCR 的诊断功能。 该功能可突出显示文本的特定部分,并将其保存为图像以供验证。 无论是构建文档处理系统、实施质量控制措施,还是验证您的 OCR 实施,该功能都能就 OCR 引擎检测到的内容提供即时的可视化反馈。

快速入门:立即高亮显示 PDF 中的单词

此片段演示了 IronOCR 的用法:加载 PDF 文件并高亮显示文档中的每个单词,然后将结果保存为图像。 只需一行即可获得 OCR 结果的视觉反馈。

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

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

    PM > Install-Package IronOcr

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

    new IronOcr.OcrInput().LoadPdf("document.pdf").HighlightTextAndSaveAsImages(new IronOcr.IronTesseract(), "highlight_page_", IronOcr.ResultHighlightType.Word);
  3. 部署到您的生产环境中进行测试

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

如何突出显示文本并另存为图像?

<! -- 待办事项:在此处添加图片 --> <! -- 屏幕截图演示如何突出显示文本并保存为图像? 在 IronPDF 中 --> <!-- 说明:显示逐步过程的截图 -->

使用 IronOCR 可以直接突出显示文本并将其保存为图片。 使用 LoadPdf 加载现有 PDF,然后调用 HighlightTextAndSaveAsImages 方法来突出显示文本部分并将其保存为图像。 该技术可验证 OCR 的准确性并调试文档中的文本识别问题。

该方法需要三个参数:IronTesseract OCR 引擎、输出文件名的前缀以及 ResultHighlightType 中的枚举,该枚举决定了要高亮显示的文本类型。 本示例使用 ResultHighlightType.Paragraph 将文本块高亮显示为段落。

[{i:(此函数使用输出字符串前缀,并为每一页的输出图像文件名添加页面标识符(如 "page_0"、"page_1")。

本示例使用的 PDF 有三个段落。

输入的 PDF 是什么样的?

如何实现突出显示代码?

下面的示例代码演示了使用 OcrInput 类的基本实现。

:path=/static-assets/ocr/content-code-examples/how-to/highlight-texts-as-images.cs
using IronOcr;

IronTesseract ocrTesseract = new IronTesseract();

using var ocrInput = new OcrInput();
ocrInput.LoadPdf("document.pdf");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_page_", ResultHighlightType.Paragraph);
Imports IronOcr

Private ocrTesseract As New IronTesseract()

Private ocrInput = New OcrInput()
ocrInput.LoadPdf("document.pdf")
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_page_", ResultHighlightType.Paragraph)
$vbLabelText   $csharpLabel

输出图像显示了什么?

带有三个段落的网页,中间段落用红色边框突出显示文本选择功能

如上图所示,所有三个段落都用浅红色方框突出显示。 这种可视化的表达方式可以帮助开发人员快速识别 OCR 引擎是如何将文档分割成可读区块的。

有哪些不同的 ResultHighlightType 选项?

上面的示例使用 ResultHighlightType.Paragraph 来突出显示文本块。 IronOCR 通过此枚举提供额外的突出显示选项。 以下是可用类型的完整列表,每种类型都有不同的诊断用途。

字符:围绕 OCR 引擎检测到的每个字符绘制一个边界框。这对于调试字符识别或专用字体非常有用,尤其是在使用 定制语言文件时。

单词:突出显示引擎识别出的每个完整单词。这是验证单词边界和正确单词识别的理想选择,尤其是在实施 条形码和 QR 阅读以及文本识别时。

:突出显示检测到的每一行文本。对于布局复杂、需要行识别验证的文档非常有用,例如在处理 扫描文档时。

段落:突出显示作为段落分组的整个文本块。非常适合理解文档布局和验证文本块分割,在使用 表格提取时尤其有用。

如何比较不同的高亮类型?

本综合示例演示了在同一文档中生成所有不同类型的高亮内容,您可以对结果进行比较:

using IronOcr;
using System;

// Initialize the OCR engine with custom configuration
IronTesseract ocrTesseract = new IronTesseract();

// Configure for better accuracy if needed
ocrTesseract.Configuration.ReadBarCodes = false; // Disable if not needed for performance
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Load the PDF document
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("document.pdf");

// Generate highlights for each type
Console.WriteLine("Generating character-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character);

Console.WriteLine("Generating word-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word);

Console.WriteLine("Generating line-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line);

Console.WriteLine("Generating paragraph-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph);

Console.WriteLine("All highlight images have been generated successfully!");
using IronOcr;
using System;

// Initialize the OCR engine with custom configuration
IronTesseract ocrTesseract = new IronTesseract();

// Configure for better accuracy if needed
ocrTesseract.Configuration.ReadBarCodes = false; // Disable if not needed for performance
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Load the PDF document
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("document.pdf");

// Generate highlights for each type
Console.WriteLine("Generating character-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character);

Console.WriteLine("Generating word-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word);

Console.WriteLine("Generating line-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line);

Console.WriteLine("Generating paragraph-level highlights...");
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph);

Console.WriteLine("All highlight images have been generated successfully!");
Imports IronOcr
Imports System

' Initialize the OCR engine with custom configuration
Dim ocrTesseract As New IronTesseract()

' Configure for better accuracy if needed
ocrTesseract.Configuration.ReadBarCodes = False ' Disable if not needed for performance
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd

' Load the PDF document
Using ocrInput As New OcrInput()
    ocrInput.LoadPdf("document.pdf")

    ' Generate highlights for each type
    Console.WriteLine("Generating character-level highlights...")
    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character)

    Console.WriteLine("Generating word-level highlights...")
    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word)

    Console.WriteLine("Generating line-level highlights...")
    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line)

    Console.WriteLine("Generating paragraph-level highlights...")
    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph)
End Using

Console.WriteLine("All highlight images have been generated successfully!")
$vbLabelText   $csharpLabel

如何处理多页文档?

在处理多页 PDF 或 多帧 TIFF 文件时,高亮功能会自动单独处理每一页。 这在实施 PDF OCR 文本提取工作流程时尤其有用:

using IronOcr;
using System.IO;

IronTesseract ocrTesseract = new IronTesseract();

// Load a multi-page document
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("multi-page-document.pdf");

// Create output directory if it doesn't exist
string outputDir = "highlighted_pages";
Directory.CreateDirectory(outputDir);

// Generate highlights for each page
// Files will be named: highlighted_pages/page_0.png, page_1.png, etc.
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, 
    Path.Combine(outputDir, "page_"), 
    ResultHighlightType.Word);

// Count generated files for verification
int pageCount = Directory.GetFiles(outputDir, "page_*.png").Length;
Console.WriteLine($"Generated {pageCount} highlighted page images");
using IronOcr;
using System.IO;

IronTesseract ocrTesseract = new IronTesseract();

// Load a multi-page document
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("multi-page-document.pdf");

// Create output directory if it doesn't exist
string outputDir = "highlighted_pages";
Directory.CreateDirectory(outputDir);

// Generate highlights for each page
// Files will be named: highlighted_pages/page_0.png, page_1.png, etc.
ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, 
    Path.Combine(outputDir, "page_"), 
    ResultHighlightType.Word);

// Count generated files for verification
int pageCount = Directory.GetFiles(outputDir, "page_*.png").Length;
Console.WriteLine($"Generated {pageCount} highlighted page images");
Imports IronOcr
Imports System.IO

Dim ocrTesseract As New IronTesseract()

' Load a multi-page document
Using ocrInput As New OcrInput()
    ocrInput.LoadPdf("multi-page-document.pdf")

    ' Create output directory if it doesn't exist
    Dim outputDir As String = "highlighted_pages"
    Directory.CreateDirectory(outputDir)

    ' Generate highlights for each page
    ' Files will be named: highlighted_pages/page_0.png, page_1.png, etc.
    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, 
                                          Path.Combine(outputDir, "page_"), 
                                          ResultHighlightType.Word)

    ' Count generated files for verification
    Dim pageCount As Integer = Directory.GetFiles(outputDir, "page_*.png").Length
    Console.WriteLine($"Generated {pageCount} highlighted page images")
End Using
$vbLabelText   $csharpLabel

什么是性能最佳实践?

使用高亮功能时,请考虑以下最佳实践:

1.文件大小:突出显示的图像可能很大,尤其是高分辨率文件。 在处理大批量文件时,请考虑输出目录的可用空间。 有关优化技巧,请参阅我们的快速 OCR 配置指南

2.性能:生成高亮显示会增加处理开销。 对于偶尔需要突出显示的生产系统,应将其作为单独的诊断流程而不是主要工作流程的一部分来实施。 考虑使用多线程 OCR 进行批处理。

3.错误处理:在进行文件操作时,始终执行正确的错误处理:

try
{
    using var ocrInput = new OcrInput();
    ocrInput.LoadPdf("document.pdf");

    // Apply image filters if needed for better recognition
    ocrInput.Deskew(); // Correct slight rotations
    ocrInput.DeNoise(); // Remove background noise

    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word);
}
catch (Exception ex)
{
    Console.WriteLine($"Error during highlighting: {ex.Message}");
    // Log error details for debugging
}
try
{
    using var ocrInput = new OcrInput();
    ocrInput.LoadPdf("document.pdf");

    // Apply image filters if needed for better recognition
    ocrInput.Deskew(); // Correct slight rotations
    ocrInput.DeNoise(); // Remove background noise

    ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word);
}
catch (Exception ex)
{
    Console.WriteLine($"Error during highlighting: {ex.Message}");
    // Log error details for debugging
}
Imports System

Try
    Using ocrInput As New OcrInput()
        ocrInput.LoadPdf("document.pdf")

        ' Apply image filters if needed for better recognition
        ocrInput.Deskew() ' Correct slight rotations
        ocrInput.DeNoise() ' Remove background noise

        ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word)
    End Using
Catch ex As Exception
    Console.WriteLine($"Error during highlighting: {ex.Message}")
    ' Log error details for debugging
End Try
$vbLabelText   $csharpLabel

高亮显示如何与 OCR 结果相结合?

高亮功能可与 IronOCR 的结果对象无缝配合,让您可以将可视化高亮与提取的文本数据关联起来。 当您需要跟踪 OCR 进度或验证已识别文本的特定部分时,这一点尤其有用。 OcrResult 类提供了有关每个检测到的元素的详细信息,这些信息与该方法生成的可视化高亮部分直接对应。

如果我遇到问题怎么办?

如果在使用高亮显示功能时遇到问题,请查阅 一般故障排除指南 了解常见的解决方案。 针对与突出显示相关的具体问题:

  • 空白输出图像:确保输入文档包含可读文本,并且 OCR 引擎已针对您的文档类型正确配置。 您可能需要应用图像优化过滤器修正图像方向来提高识别率。
  • 遗漏要点:某些文档类型可能需要进行特定的预处理。 尝试应用图像过滤器修正图像方向以提高识别率。
  • 性能问题:对于大型文档,请考虑实施 多线程 以提高处理速度。 此外,如果使用质量较差的输入,请查看我们的修复低质量扫描指南。

如何将其用于生产调试?

高亮功能可作为出色的生产调试工具。 当与用于长期运行操作的中止令牌超时集成时,您可以创建一个强大的诊断系统。 考虑在您的应用程序中实施调试模式:

public class OcrDebugger
{
    private readonly IronTesseract _tesseract;
    private readonly bool _debugMode;

    public OcrDebugger(bool enableDebugMode = false)
    {
        _tesseract = new IronTesseract();
        _debugMode = enableDebugMode;
    }

    public OcrResult ProcessDocument(string filePath)
    {
        using var input = new OcrInput();
        input.LoadPdf(filePath);

        // Apply preprocessing
        input.Deskew();
        input.DeNoise();

        // Generate debug highlights if in debug mode
        if (_debugMode)
        {
            string debugPath = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_";
            input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word);
        }

        // Perform actual OCR
        return _tesseract.Read(input);
    }
}
public class OcrDebugger
{
    private readonly IronTesseract _tesseract;
    private readonly bool _debugMode;

    public OcrDebugger(bool enableDebugMode = false)
    {
        _tesseract = new IronTesseract();
        _debugMode = enableDebugMode;
    }

    public OcrResult ProcessDocument(string filePath)
    {
        using var input = new OcrInput();
        input.LoadPdf(filePath);

        // Apply preprocessing
        input.Deskew();
        input.DeNoise();

        // Generate debug highlights if in debug mode
        if (_debugMode)
        {
            string debugPath = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_";
            input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word);
        }

        // Perform actual OCR
        return _tesseract.Read(input);
    }
}
Imports System.IO

Public Class OcrDebugger
    Private ReadOnly _tesseract As IronTesseract
    Private ReadOnly _debugMode As Boolean

    Public Sub New(Optional enableDebugMode As Boolean = False)
        _tesseract = New IronTesseract()
        _debugMode = enableDebugMode
    End Sub

    Public Function ProcessDocument(filePath As String) As OcrResult
        Using input As New OcrInput()
            input.LoadPdf(filePath)

            ' Apply preprocessing
            input.Deskew()
            input.DeNoise()

            ' Generate debug highlights if in debug mode
            If _debugMode Then
                Dim debugPath As String = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_"
                input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word)
            End If

            ' Perform actual OCR
            Return _tesseract.Read(input)
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

下一步我应该去哪里?

<! -- 待办事项:在此处添加图片 --> <! --介绍实现的示意图 --> <!--说明:说明代码概念的图表或截图 -->

现在您已了解如何使用高亮显示功能,请继续探索:

如需用于生产,请务必获取许可证以去除水印并使用全部功能。

常见问题解答

如何在我的 C# 应用程序中可视化 OCR 结果?

IronOCR 提供了 HighlightTextAndSaveAsImages 方法,该方法通过在检测到的文本元素(字符、单词、行或段落)周围绘制边框来实现 OCR 结果的可视化,并将其保存为诊断图像。该功能可帮助开发人员验证 OCR 的准确性并调试识别问题。

在 PDF 文档中突出显示单词的最简单方法是什么?

使用 IronOCR,您只需一行代码就能高亮 PDF 中的单词:new IronOcr.OcrInput().LoadPdf("document.pdf").HighlightTextAndSaveAsImages(new IronOcr.IronTesseract(), "highlight_page_", IronOcr.ResultHighlightType.Word).这将加载 PDF 并创建带有高亮单词的图像。

HighlightTextAndSaveAsImages 方法需要哪些参数?

IronOCR 中的 HighlightTextAndSaveAsImages 方法需要三个参数:IronTesseract OCR 引擎实例、输出文件名的前缀字符串和 ResultHighlightType 枚举值,后者用于指定要高亮显示的文本元素(字符、单词、行或段落)。

使用文本高亮时,输出图像如何命名?

IronOCR 通过将您指定的前缀与页面标识符相结合,自动为输出图像命名。例如,如果使用 "highlight_page_"作为前缀,该方法将为文档中的每一页生成名为 "highlight_page_0"、"highlight_page_1 "等的文件。

为什么可视化高亮对 OCR 开发很重要?

IronOCR 中的可视化高亮显示通过准确显示 OCR 引擎检测到的文本以及潜在错误发生的位置,提供了至关重要的诊断反馈。这种可视化地图可帮助开发人员调试识别问题,验证 OCR 的准确性,并排除复杂文档中的故障。

除了单词,我还能突出显示不同类型的文本元素吗?

是的,IronOCR 的 ResultHighlightType 枚举允许您突出显示各种文本元素,包括单个字符、单词、行或整个段落。只需在调用 HighlightTextAndSaveAsImages 方法时指定所需的类型,即可直观地看到不同级别的文本检测。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 5,384,824 | 版本: 2026.2 刚刚发布