Highlight Texts for Debugging

IronOCR includes built‑in support to visually highlight OCR-detected elements (characters, words, lines, or paragraphs) on images or pages, and export them as PNGs for debugging.

using IronOcr;
using System;

class HighlightTextDebugging
{
    static void Main()
    {
        using var ocr = new IronTesseract();
        using var input = new OcrInput();

        // Load a PDF or image file
        input.AddImage("sample_image.png");
        // Alternatively: input.LoadPdf("document.pdf");

        // Perform OCR to initialize detection
        var result = ocr.Read(input);

        // Highlight paragraphs and output images with bounding boxes
        // e.g. "highlight_page_" yields files like highlight_page_1.png, ...
        input.HighlightTextAndSaveAsImages(
            ocr, 
            "highlight_page_", 
            ResultHighlightType.Paragraph
        );

        Console.WriteLine("Saved images with highlighted text regions for debugging.");
    }
}
using IronOcr;
using System;

class HighlightTextDebugging
{
    static void Main()
    {
        using var ocr = new IronTesseract();
        using var input = new OcrInput();

        // Load a PDF or image file
        input.AddImage("sample_image.png");
        // Alternatively: input.LoadPdf("document.pdf");

        // Perform OCR to initialize detection
        var result = ocr.Read(input);

        // Highlight paragraphs and output images with bounding boxes
        // e.g. "highlight_page_" yields files like highlight_page_1.png, ...
        input.HighlightTextAndSaveAsImages(
            ocr, 
            "highlight_page_", 
            ResultHighlightType.Paragraph
        );

        Console.WriteLine("Saved images with highlighted text regions for debugging.");
    }
}
Imports IronOcr
Imports System

Friend Class HighlightTextDebugging
	Shared Sub Main()
		Dim ocr = New IronTesseract()
		Dim input = New OcrInput()

		' Load a PDF or image file
		input.AddImage("sample_image.png")
		' Alternatively: input.LoadPdf("document.pdf");

		' Perform OCR to initialize detection
		Dim result = ocr.Read(input)

		' Highlight paragraphs and output images with bounding boxes
		' e.g. "highlight_page_" yields files like highlight_page_1.png, ...
		input.HighlightTextAndSaveAsImages(ocr, "highlight_page_", ResultHighlightType.Paragraph)

		Console.WriteLine("Saved images with highlighted text regions for debugging.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation

  • OCR-based region detection replaces manual rectangle definitions
  • HighlightTextAndSaveAsImages(ocr, prefix, highlightType) overlays red boxes around detected elements — paragraphs, lines, words, or characters — and saves each page as an image.
  • Uses ResultHighlightType to choose what to highlight: Character, Word, Line, or Paragraph.
  • Handles detection and drawing internally — much simpler than manually specifying rectangle coordinates.

Optional Manual Approach via StampCropRectangles

If you want to manually handle bounding boxes from OCR results:

using IronOcr;
using System;
using System.Drawing;

class ManualHighlightingWithRectangles
{
    static void Main()
    {
        using var ocr = new IronTesseract();
        using var input = new OcrInput("sample_image.png");
        var result = ocr.Read(input);

        // Get recognized word rectangles
        var rects = result.WordBounds;

        // Stamp and save manual rectangle highlights
        // using System.Drawing.Color.Red
        var files = input.StampCropRectanglesAndSaveAs(
            rects.ToArray(), 
            System.Drawing.Color.Red, 
            "manual_highlight_", 
            AnyBitmap.ImageFormat.Png
        );

        Console.WriteLine($"Saved {files.Length} debug images: {string.Join(", ", files)}");
    }
}
using IronOcr;
using System;
using System.Drawing;

class ManualHighlightingWithRectangles
{
    static void Main()
    {
        using var ocr = new IronTesseract();
        using var input = new OcrInput("sample_image.png");
        var result = ocr.Read(input);

        // Get recognized word rectangles
        var rects = result.WordBounds;

        // Stamp and save manual rectangle highlights
        // using System.Drawing.Color.Red
        var files = input.StampCropRectanglesAndSaveAs(
            rects.ToArray(), 
            System.Drawing.Color.Red, 
            "manual_highlight_", 
            AnyBitmap.ImageFormat.Png
        );

        Console.WriteLine($"Saved {files.Length} debug images: {string.Join(", ", files)}");
    }
}
Imports IronOcr
Imports System
Imports System.Drawing

Friend Class ManualHighlightingWithRectangles
	Shared Sub Main()
		Dim ocr = New IronTesseract()
		Dim input = New OcrInput("sample_image.png")
		Dim result = ocr.Read(input)

		' Get recognized word rectangles
		Dim rects = result.WordBounds

		' Stamp and save manual rectangle highlights
		' using System.Drawing.Color.Red
		Dim files = input.StampCropRectanglesAndSaveAs(rects.ToArray(), System.Drawing.Color.Red, "manual_highlight_", AnyBitmap.ImageFormat.Png)

		Console.WriteLine($"Saved {files.Length} debug images: {String.Join(", ", files)}")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • result.WordBounds (or .LineBounds, .ParagraphBounds) yields Rectangle[] with detected regions.
  • StampCropRectanglesAndSaveAs(...) visualizes those rectangles and writes images to disk