OCR
Recognise scanned pages and photos on iOS and Android, in Latin and, with neural models, any other script, and add an invisible, searchable text layer with NexPDF for React Native.
License module
OCR needs the ocr module (Enterprise tier).
runOcr(options) recognises the text of scanned pages and adds it as an invisible text layer, so the
PDF becomes searchable and its text can be extracted. The page images are not changed. Recognition
runs on the device.
const doc = await NexPdf.api.openImage(photoBase64); // or NexPdf.open(scannedPdfBase64)
const report = await doc.runOcr({ languages: 'por+eng' });
console.log(report.engine, report.pages, report.words, report.confidence);
const searchable = await doc.saveBytes();ocr(optionsJson) is the same call with the JSON string in and out.
Options
| Option | Default | Meaning |
|---|---|---|
engine | auto | builtin, tesseract, neural, or auto: a loaded neural model that reads the requested languages, else Tesseract when it is installed with every requested language, else the built-in engine |
languages | por+eng+spa | Language codes joined with + (Tesseract codes such as chi_sim or ISO 639-1 codes such as ar). The built-in engine reads Latin-script text in any language |
dpi | 300 | Resolution the pages are rendered at for recognition |
pages | every page | 0-based page indices |
force | false | Also process pages that already have text |
Phones have no Tesseract program, so auto uses a loaded neural model when one reads the requested
languages, else the built-in engine, which needs nothing else.
Other scripts: neural models
The built-in engine reads printed Latin-script text (English, Portuguese, Spanish, French, German and others) with no models. For Chinese, Japanese, Korean, Arabic, Persian, Urdu, Cyrillic, Devanagari, Thai, Greek and other scripts, load neural OCR models at run time. No model is bundled: your app downloads or ships 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):
| File | Use | languages |
|---|---|---|
detection/v3/det.onnx (2.4 MB) | Detection, mobile | all |
detection/v5/det.onnx (88 MB) | Detection, server | all |
languages/chinese/rec.onnx + dict.txt | Chinese, Japanese | zh+ja+en |
languages/korean/… | Korean | ko |
languages/arabic/… | Arabic, Persian, Urdu | ar+fa+ur |
languages/eslav/… | Cyrillic | ru+uk+be+bg |
languages/hindi/… | Devanagari | hi+mr+ne+sa |
The PaddleOCR model zoo (converted with paddle2onnx) and RapidOCR publish the same models. The
dictionary must be the one the model was trained with; loading checks it.
On a phone, use the mobile models (a few MB). Ship them in the app (on iOS in the app bundle, on
Android in assets/ with .onnx marked noCompress in Gradle) or download them once into the
app's documents or Application Support folder. Read the model as base64 with your file-system
library and the dictionary as UTF-8 text, and load them once at start-up:
import { NexPdf } from '@nexdevit/nexpdf-react-native';
await NexPdf.api.ocrLoadModel('{"kind":"detection"}', detOnnxBase64, null);
await NexPdf.api.ocrLoadModel('{"kind":"recognition","languages":"ar+fa+ur","name":"arabic"}',
arabicRecOnnxBase64, arabicDictText);
console.log(JSON.parse(await NexPdf.api.ocrModelsJson())); // kind, name, languages, symbols, size
const doc = await NexPdf.open(scanArBase64);
const report = await doc.runOcr({ languages: 'ara' });
console.log(report.engine); // "neural (arabic)"
const searchable = await doc.saveBytes();
await NexPdf.api.ocrUnloadModels('ar'); // or null for every modelModels are loaded once per app process and used by every document. A model crosses the bridge as
base64 once, when it is loaded. ocrLoadModel options:
| Option | Meaning |
|---|---|
kind | detection, recognition or classification (required) |
languages | Languages a recognition model reads, joined with +, as ISO 639-1 (ar) or Tesseract (ara) codes |
name | Name in listings; loading the same kind and name again replaces the model |
input_height | Recognition input height when the model leaves it open (default 48) |
max_side | Detection: longest side, in pixels, of the page image the detector sees (default 1536) |
An optional classification model is the PaddleOCR cls text-direction model (0 or 180
degrees, input 3×48×192). It takes no dictionary. Without it, upright and upside-down lines are
told apart by the recogniser's confidence both ways.
await NexPdf.api.ocrLoadModel('{"kind":"classification"}', clsOnnxBase64, null);With several languages ("rus+chi_sim") every matching model reads each line and the most confident
reading wins. Mobile models are fast; server models are more accurate and much larger.
The neural engine reads slanted lines as rotated boxes (the text layer follows the slant). Arabic word spaces are restored from the gaps in the ink, presentation forms are mapped to plain letters and the text is stored in logical order. Vertical CJK columns are read right to left, in the report and in the text layer, and text extraction reads vertical text as columns from right to left, so the text extracts, searches and copies in reading order.
Orientation and skew
Pages scanned sideways, upside down or slightly skewed are corrected: the orientation (0, 90, 180 or 270 degrees) and the skew are detected, the page is read upright, and the text layer is mapped back onto the original page. The built-in engine corrects skew up to 20 degrees, the neural engine up to 15 degrees either way (besides quarter turns). The page images are not changed.
The built-in engine also reads ß, œ, æ, ÿ, ì, Ò, Ù and the Catalan middle dot, and ignores scanner specks.
Limits
- Curved lines are read only when the curve is gentle.
- With the neural engine, skew beyond 15 degrees is not corrected.
- The OCR text layer uses glyph-less fonts, so convert to PDF/A after OCR when the file must be PDF/A.
The report
| Field | Meaning |
|---|---|
engine | The engine that ran |
pages | Pages recognised |
skipped | Pages skipped because they already had text |
words | Words recognised |
confidence | Average word confidence, 0 to 1 |
text | The recognised text, one string per page |