Skip to Content
NexPDF

OCR

Make scanned documents shown in a .NET MAUI PdfView searchable with the NexPDF built-in OCR engine.

English

License module

OCR needs the ocr module (Enterprise tier).

Ocr(optionsJson) recognises the text of scanned pages and adds it as an invisible text layer; the page images are not changed. On phones and tablets the built-in engine runs (it reads Latin-script text in any language), or, since 1.1, neural models you load for other scripts; Tesseract is only used where the tesseract program is installed.

using System.Text.Json.Nodes;

var doc = view.Document!;
var report = JsonNode.Parse(await Task.Run(() => doc.Ocr("""{"engine":"builtin","dpi":300}""")))!;
StatusLabel.Text = $"{report["words"]} words, confidence {report["confidence"]}";

await view.ReloadAsync();
doc.Save(Path.Combine(FileSystem.AppDataDirectory, "searchable.pdf"));

OCR is CPU-heavy: keep it on a background thread as above, and limit it with "pages":[0,1] when you need only some pages. Options and the report fields are listed in the .NET OCR guide.

Other scripts: neural models

The built-in engine reads printed Latin-script text with no models. For Chinese, Japanese, Korean, Arabic, Persian, Urdu, Cyrillic, Devanagari, Thai, Greek and other scripts, load neural OCR models at run time with NexPdfSdk.OcrLoadModel. No model is bundled: your app ships or downloads the ones it needs.

You need one text detection model, which serves every language, and one recognition model per script with its character dictionary (UTF-8 text, one symbol per line). Any PaddleOCR detection and CTC recognition model exported to ONNX works (PP-OCRv3, v4 and v5); a ready-made set is monkt/paddleocr-onnx on Hugging Face (Apache-2.0). On phones use the mobile models: detection/v3/det.onnx is 2.4 MB, while the server detection model is 88 MB. The file list is in the .NET OCR guide.

Ship the files in Resources/Raw (they become MauiAsset items), or download them once into FileSystem.AppDataDirectory, and load them at start-up, off the UI thread:

using Nexdevit.NexPdf;

static async Task<byte[]> ReadAssetAsync(string name)
{
    await using var s = await FileSystem.OpenAppPackageFileAsync(name);
    using var ms = new MemoryStream();
    await s.CopyToAsync(ms);
    return ms.ToArray();
}

var det = await ReadAssetAsync("ocr/det.onnx");
var rec = await ReadAssetAsync("ocr/arabic/rec.onnx");
var dict = System.Text.Encoding.UTF8.GetString(await ReadAssetAsync("ocr/arabic/dict.txt"));

await Task.Run(() =>
{
    NexPdfSdk.OcrLoadModel("""{"kind":"detection"}""", det);
    NexPdfSdk.OcrLoadModel("""{"kind":"recognition","languages":"ar+fa+ur","name":"arabic"}""", rec, dict);
});

var report = JsonNode.Parse(await Task.Run(() => view.Document!.Ocr("""{"languages":"ara"}""")))!;
StatusLabel.Text = report["engine"]!.ToString();   // "neural (arabic)"
await view.ReloadAsync();

On Android, keep .onnx files uncompressed in the APK so they are not inflated on every read, for example with <AndroidStoreUncompressedFileExtensions>.onnx</AndroidStoreUncompressedFileExtensions> in the project file.

Models are loaded once per process and used by every document. NexPdfSdk.OcrModelsJson() lists them (kind, name, languages, symbols, size) and NexPdfSdk.OcrUnloadModels(languages) frees them (null for all), which returns their memory on a phone. Options of OcrLoadModel: kind (detection, recognition or classification, required), languages (ISO 639-1 such as ar or Tesseract codes such as ara, joined with +), name, input_height and max_side.

A classification model is the optional PaddleOCR cls text-direction model (0° or 180°, input 3×48×192), loaded with no dictionary:

var cls = await ReadAssetAsync("ocr/cls.onnx");
await Task.Run(() => NexPdfSdk.OcrLoadModel("""{"kind":"classification"}""", cls));

Without it, the recogniser reads each line both ways and the more confident reading decides whether it is upside down.

Right-to-left lines are written in logical order (Arabic word spaces restored, presentation forms mapped to plain letters) and vertical CJK columns are read top to bottom, columns right to left, so the text extracts, searches and copies in reading order. Slanted lines are read along their slant.

Rotated and skewed scans

Pages photographed or scanned sideways, upside down or slightly crooked are read upright: the engine finds the orientation (0°, 90°, 180° or 270°) and the skew and maps the text layer back onto the original page. The built-in engine corrects skew up to 20°, the neural engine up to 15° (besides quarter turns). Only gentle curves in curved lines are followed.

The OCR text layer uses glyph-less fonts, so convert to PDF/A after OCR when the file must be PDF/A.

On this page