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— aList<RecognitionResult.LinesResult>, one entry per detected text line, each withTextInLine(the line’s text) andLine(aSystem.Drawing.Rectanglegiving 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:
| Condition | Exception |
|---|---|
input is null | ArgumentNullException |
input’s InputType is not SingleImage or PDF | NotSupportedException |
A path added via OcrInput.Add() does not exist on disk | FileNotFoundException |
These checks run before any model inference, so a bad input fails immediately rather than after a partial recognition pass.
Tips and Best Practices
OcrOutputis empty (Count == 0) ifOcrInputhas no files added — checkCountbefore indexing.RecognitionResult.LinesResultis nested insideRecognitionResult; the top-level type nameLinesResultalone is not a resolvable type outside that nesting.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
ArgumentNullException on Recognize() | null was passed instead of an OcrInput | Always construct an OcrInput before calling Recognize() |
NotSupportedException on Recognize() | OcrInput was constructed with an input type other than SingleImage/PDF | Only InputType.SingleImage and InputType.PDF are valid |
FileNotFoundException on Recognize() | A queued path does not exist | Validate 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/Method | Description |
|---|---|
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) |
OcrOutput | List<RecognitionResult> returned by Recognize() |
RecognitionResult.RecognitionText | Full recognized text for one image/page |
RecognitionResult.RecognitionLinesResult | List<RecognitionResult.LinesResult> |
RecognitionResult.LinesResult.TextInLine | Recognized text for one line |
RecognitionResult.LinesResult.Line | System.Drawing.Rectangle bounding box for one line |