Core API

Core API

This page covers the object lifecycle and exception behavior of the four core types: AsposeOcr, OcrInput, OcrOutput, and RecognitionResult.


OcrInput Lifecycle

OcrInput implements IDisposable and should be created in a using statement. Its constructor takes an InputType (InputType.SingleImage or InputType.PDF), and OcrInput.Add(string) queues one file path at a time — the same OcrInput instance can hold multiple files of that same input type.

using Aspose.OCR;

using var input = new OcrInput(InputType.SingleImage);
input.Add("page-1.jpg");
input.Add("page-2.png");

Recognizing and Reading OcrOutput

AsposeOcr.Recognize(OcrInput) returns an OcrOutput, which is a List<RecognitionResult> — indexable like any list, with one RecognitionResult per image (for InputType.SingleImage, one per added file) or per PDF page (for InputType.PDF).

var api = new AsposeOcr();
OcrOutput output = api.Recognize(input);
RecognitionResult first = output[0];

RecognitionResult and the Nested LinesResult Type

RecognitionResult.LinesResult is a nested class — reference it as RecognitionResult.LinesResult, not as a standalone type. Each RecognitionResult exposes:

  • RecognitionText — the complete recognized text for that image or page, with lines joined by line breaks.
  • RecognitionLinesResult — a List<RecognitionResult.LinesResult>, one entry per detected text line, each with TextInLine (the line’s text) and Line (a System.Drawing.Rectangle giving that line’s pixel coordinates in the source image).
RecognitionResult result = output[0];
Console.WriteLine(result.RecognitionText);

foreach (RecognitionResult.LinesResult line in result.RecognitionLinesResult)
{
    System.Drawing.Rectangle bounds = line.Line;
    Console.WriteLine($"x={bounds.X}, y={bounds.Y}: {line.TextInLine}");
}

Exceptions

AsposeOcr.Recognize(OcrInput) validates its input before running recognition:

ConditionException
input is nullArgumentNullException
input’s InputType is not SingleImage or PDFNotSupportedException
A path added via OcrInput.Add() does not exist on diskFileNotFoundException

These checks run before any model inference, so a bad input fails immediately rather than after a partial recognition pass.


Tips and Best Practices

  • OcrOutput is empty (Count == 0) if OcrInput has no files added — check Count before indexing.
  • RecognitionResult.LinesResult is nested inside RecognitionResult; the top-level type name LinesResult alone is not a resolvable type outside that nesting.

Common Issues

IssueCauseFix
ArgumentNullException on Recognize()null was passed instead of an OcrInputAlways construct an OcrInput before calling Recognize()
NotSupportedException on Recognize()OcrInput was constructed with an input type other than SingleImage/PDFOnly InputType.SingleImage and InputType.PDF are valid
FileNotFoundException on Recognize()A queued path does not existValidate file paths before calling OcrInput.Add()

FAQ

Is RecognitionResult.LinesResult its own top-level class?

No — it is nested inside RecognitionResult. Reference it as RecognitionResult.LinesResult.

Does OcrOutput guarantee page order for PDFs?

Yes — for InputType.PDF, OcrOutput returns one RecognitionResult per page in source order.

What happens if I call Recognize() with an empty OcrInput?

It returns an OcrOutput with Count == 0 rather than throwing.


API Reference Summary

Class/MethodDescription
AsposeOcr.Recognize(OcrInput)Validates and recognizes; throws ArgumentNullException, NotSupportedException, or FileNotFoundException on invalid input
OcrInput.OcrInput(InputType)Constructs an input container for one input type
OcrInput.Add(string)Queues a file path
OcrInput.Dispose()Releases resources (via IDisposable)
OcrOutputList<RecognitionResult> returned by Recognize()
RecognitionResult.RecognitionTextFull recognized text for one image/page
RecognitionResult.RecognitionLinesResultList<RecognitionResult.LinesResult>
RecognitionResult.LinesResult.TextInLineRecognized text for one line
RecognitionResult.LinesResult.LineSystem.Drawing.Rectangle bounding box for one line

See Also