Skip to Content
NexPDF

OCR

Let users recognise scanned pages and add an invisible, searchable text layer in an Electron window with NexPDF.

English

License module

OCR needs the ocr module (Enterprise tier).

OCR in the Convert mode (ai) reads the text of scanned pages and adds it as an invisible text layer, so the document can be searched, selected and copied. The page images are not changed.

The OCR dialog takes:

OptionMeaning
PagesAll pages or the current page
EngineAutomatic (Tesseract when it is installed with these languages), or the NexPDF built-in engine (offline)
LanguagesTesseract language codes such as por+eng. The built-in engine reads any Latin-script language
Also process pages that already have textRuns OCR on pages with text too

When it finishes it shows the number of words recognised, the engine that ran, the confidence and the pages skipped.

Using Tesseract

The built-in engine needs nothing else. The engine runs natively on the user's machine, so it can use Tesseract when the tesseract program and its language data are installed there (for example apt install tesseract-ocr tesseract-ocr-por).

Other scripts: neural models

The built-in engine reads printed Latin-script text (English, Portuguese, Spanish, French, German and others) with no models. In the window, the engine runs natively, so Tesseract reads other scripts when its language data is installed on the user's machine (for example chi_sim, ara or rus); pass those codes in Languages.

NexPDF 1.1 can also read Chinese, Japanese, Korean, Arabic, Cyrillic, Devanagari and other scripts with neural OCR models (PaddleOCR detection, recognition and optional classification text-direction models in ONNX) loaded at run time; see the Python OCR guide for the models. The window's engine has no way to load a model. The document API in the main process does: load the models with ocrLoadModel from documents(), and doc.ocr() (engine auto, the default) uses them when the languages match:

const fs = require('node:fs');
const { documents, openDocumentFile } = require('@nexdevit/nexpdf-electron');
const { ocrLoadModel, ocrModelsJson } = documents();

ocrLoadModel('{"kind":"detection"}', fs.readFileSync('models/det.onnx'), null);
ocrLoadModel('{"kind":"recognition","languages":"ar+fa+ur","name":"arabic"}',
  fs.readFileSync('models/arabic/rec.onnx'), fs.readFileSync('models/arabic/dict.txt', 'utf8'));
ocrLoadModel('{"kind":"classification"}', fs.readFileSync('models/cls.onnx'), null);   // optional, no dictionary
console.log(JSON.parse(ocrModelsJson()));

const doc = await openDocumentFile('scan-ar.pdf');
console.log(doc.ocr({ languages: 'ara' }).engine);   // "neural (arabic)"
await doc.save('searchable-ar.pdf');

The classification model tells upright from upside-down lines (0 or 180 degrees); without it, the recogniser's confidence both ways decides. The options of ocrLoadModel are listed in the Node.js OCR guide.

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 OCR text layer uses glyph-less fonts, so convert to PDF/A after OCR when the file must be PDF/A.

From code

To OCR in code, for example on files dropped in a folder, use the document API in the main process. It runs the built-in engine, or loaded neural models (WebAssembly has no Tesseract), and the ocr module must be activated for it:

const { openDocumentFile } = require('@nexdevit/nexpdf-electron');

const doc = await openDocumentFile('scan.pdf');
const report = doc.ocr({ languages: 'por+eng' });
console.log(report.engine, report.words, report.confidence);
await doc.save('searchable.pdf');
doc.close();

OCR blocks the thread it runs on; for many pages, run it in a worker. See the Node.js OCR guide for the options and the report.

On this page