How to Extract Text from Defined Rectangular Areas Using OCR in GroupDocs.Parser for .NET

Ever stared at a scanned document and wondered how you could just grab that specific section of text locked in a box or label? What if you could point to a rectangle on that image and say, “Hey, OCR, just read this part”? Well, that’s exactly what you’re going to learn today.

In this hands-on tutorial, we’re diving into OCR text extraction using GroupDocs.Parser for .NET — and not just any OCR. We’re talking about defining precise rectangular areas so you extract exactly what you want, nothing more, nothing less.

This guide will walk you through every step like we’re building it side by side. By the end, you’ll know how to extract text from targeted image areas with pinpoint accuracy. Ready to dive in?

Prerequisites

Before we jump into the code, let’s make sure your setup is ready to rock. Here’s what you’ll need:

Import Packages

Start by importing the necessary namespaces. These give you access to all the OCR and parsing functionality:

using System;
using System.IO;
using GroupDocs.Parser;
using GroupDocs.Parser.Data;
using GroupDocs.Parser.Options;
using GroupDocs.Parser.Ocr;

Step-by-Step Guide: Extracting OCR Text from a Defined Rectangle

Let’s break the full example down piece by piece so you really get what’s going on.

Step 1: Initialize OCR Settings with Aspose.OCR

You can’t perform OCR without telling GroupDocs how to do it. Here, we’re hooking it up with Aspose.OCR on-premise.

ParserSettings settings = new ParserSettings(new AsposeOcrOnPremise());

💡 Why this matters:

You’re telling the parser, “Hey, don’t just look at the file. If there’s an image with text, use OCR and decode it!” Aspose.OCR is like the brain here that can understand those scanned text blobs.

Step 2: Load the File with Parser and OCR Settings

Now that our OCR engine is ready, let’s load a document — say, a scanned invoice or ID card.

using (Parser parser = new Parser(Constants.SampleScan, settings))

What’s happening here?

You’re creating a new parser object and feeding it a file, while also passing the OCR engine settings so it knows how to handle image-based text.

Step 3: Define the Area to Extract Text From

We don’t want the entire page — just a part of it. Let’s say we want to extract text from a 400x200 rectangle starting at the top-left corner.

OcrOptions ocrOptions = new OcrOptions(new Data.Rectangle(0, 0, 400, 200));

🔍 Why this is crucial:

OCR can be time-consuming if you’re scanning the whole image. This step allows us to focus only on what’s necessary, like targeting just the name field on a form. Imagine using a scalpel instead of a sledgehammer — precision is everything.

Step 4: Set OCR Options in TextOptions

This step tells GroupDocs, “Hey, we want to extract text using OCR, and we’re only looking at the defined rectangle.”

TextOptions options = new TextOptions(false, true, ocrOptions);

🧠 What do those parameters mean?

  • false: Don’t extract text from the entire document
  • true: Use OCR for extraction
  • ocrOptions: Only look at our defined rectangle

Boom. That’s laser focus.

Step 5: Perform Text Extraction

Now it’s time to grab that sweet, sweet text from our image.

using (TextReader reader = parser.GetText(options))
{
    Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd());
}

🎯 Final goal:

This reads the text within that rectangle using OCR and prints it to the console. If it doesn’t support text extraction (for example, if the image is blank or unreadable), it lets you know.

Conclusion

And that’s a wrap! You’ve just learned how to perform OCR on specific rectangular areas using GroupDocs.Parser for .NET. This technique is a game-changer when you need targeted text extraction — like pulling out names, dates, or reference numbers from forms, IDs, or invoices.

Think about how much faster and cleaner this is compared to processing an entire page every time. It’s like going straight for the treasure without digging up the whole field.

With just a few lines of code, you’ve got full control over what gets read and when. Efficient, smart, and scalable.

FAQs

1. Can I extract multiple rectangular areas in a single pass?

Yes, but you’ll need to define multiple OcrOptions and loop through them, extracting each block separately.

2. What image formats are supported for OCR with GroupDocs.Parser?

Common formats like PNG, JPG, TIFF, and scanned PDFs are fully supported.

3. Does GroupDocs.Parser include OCR functionality out of the box?

No. You need to plug in an OCR engine like Aspose.OCR or any other supported connector.

4. Can I use OCR on PDFs that have mixed content (text and image)?

Absolutely! Just make sure you point the OCR engine to the right section — the image parts.

5. Is it possible to use a cloud-based OCR engine instead of an on-premise one?

Currently, GroupDocs.Parser supports on-premise OCR engines like Aspose.OCR, but cloud integration might require custom connectors.