OCR workflows: output, language & quality on Server/Desktop

Requirements
View Demo

IRIS OCR module

If only one OCR module is present (either the IRIS or default or alternative OCR module), Apryse SDK will use that module automatically (license permitting). When multiple OCR modules are present, the IRIS module can be selected using the OCR options object: `OCROptions.setEngine("iris")`.

To make a searchable PDF by adding invisible text to an image using OCR.

1PDFDoc doc;
2
3// Run OCR on the image without options
4OCRModule::ImageToPDF(doc, image_path, NULL);

Convert images to PDF with searchable/selectable text
Full code sample which shows how to use the Apryse OCR module on scanned documents in multiple languages. The OCR module can make searchable PDFs and extract scanned text for further indexing. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.

Process a scanned document

To make a searchable PDF by adding invisible text to an image based PDF such as a scanned document using OCR.

1PDFDoc doc(filename);
2
3// Set English as the language of choice
4OCROptions opts;
5opts.AddLang("eng");
6
7// Run OCR on the PDF with options
8OCRModule::ProcessPDF(doc, &opts);

Add searchable/selectable text to an image based PDF like a scanned document
Full code sample which shows how to use the Apryse OCR module on scanned documents in multiple languages. The OCR module can make searchable PDFs and extract scanned text for further indexing. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB

Get metadata as JSON

If you want to apply raw OCR output to the input document, you can either call OCRModule::ImageToPDF (if input file is an image) or OCROptions::ProcessPDF (for a PDF). However, it is likely that some post-processing will be beneficial, e.g., comparing results against white/black lists. To this purpose, you can first extract text and corresponding metadata as either JSON or XML before re-applying processed results to the input document.

1// Setup empty destination doc
2PDFDoc doc;
3
4std:string image_path = "path/to/image";
5
6// Extract OCR results as JSON
7UString json = OCRModule::GetOCRJsonFromImage(doc, image_path, opts);
8
9// Post-processing step (whatever it might be)
10
11// Re-apply results.
12OCRModule::ApplyOCRJsonToPDF(doc, json);

Output Attributes

OCR output consists of nested arrays: array of pages, array of paragraphs, array of lines, array of words. Pages have additional metadata:

Attribute

Value

Description

num

page number

dpi

document resolution (needed to correctly scale the coordinates from points to pixels)

origin

TopLeft

coordinate system has origin at the top left corner (default)

BottomLeft

coordinate system has origin at the bottom left corner (i.e., PDF page coordinate system)

Then each word in the OCR output has the following:

Attribute

Value

Description

x

bouding box lower left corner x coordinate

y

bouding box lower left corner y coordinate

length

length of bounding box

font-size

text's font size

text

text output

orientation

L

270 degrees clockwise rotation

R

90 degrees clockwise rotation

D

180 degrees clockwise rotation

U

0 degrees clockwise rotation

Finally, each line has an optional box property consisting of 4 values having the same interpretation as pdftron::PDF::Rect.

Sample JSON output

Below is a sample JSON output that the OCR module would output.

JSON

1{
2 "Page":[
3 {
4 "Para":[
5 {
6 "Line":[
7 {
8 "Word":[
9 {
10 "font-size": 27,
11 "length": 64,
12 "orientation": "U",
13 "text":"Hello",
14 "x": 273,
15 "y": 265
16 }
17 ],
18 "box":[
19 273,
20 265,
21 64,
22 29
23 ]
24 }
25 ]
26 }
27 ],
28 "num": 1,
29 "dpi": 96,
30 "origin": "BottomLeft"
31 }
32 ]
33}

External OCR results

The API can also be used to apply OCR XML/JSON generated by different OCR engines. The expected structure for input JSON and XML respectively are:

JSON

1{
2 "Page":[
3 {
4 "Word":[
5 {
6 "font-size": 12,
7 "length": 43,
8 "text":"ABC",
9 "x": 321,
10 "y": 141
11 }
12 ],
13 "num": 1,
14 "dpi": 96,
15 "origin": "TopLeft"
16 }
17 ]
18}

XML

1<Doc>
2 <Page num="1" origin="TopLeft" dpi="96">
3 <Word font-size="12" x="321" y="141" length="43">ABC</Word>
4 </Page>
5</Doc>

Note that the OCR structure is simplified and we are expecting an array of Page, with each page consisting of Word array. Each Word is described by its text content and 4 typographic point values (i.e., font-size="12" x="321" y="141" length="43" in the example above) needed to construct the bounding box for placement of text on a page.

Language options

You use pdftron.PDF.OCROptions convenience class to pass OCR parameters. You can call pdftron.PDF.OCROptions.AddLang to pick a target language. If no language option is set, English is assumed.

You can review the lists of supported languages for the default, alternative, and IRIS OCR.

Only one of the Chinese (traditional), Chinese (simplified), Japanese and Korean can be selected at the same time

Adding languages to the alternative OCR module

Additional trained language files can be placed in the search path ( which can be registered using PDFNet::AddResourceSearchPath ). Afterwards, they can be referred to via their file prefix.

Multiple languages

Multiple languages can be specified, although it is not recommended to use more than 3 languages.

1// Add French, Spanish and default English to target languages
2OCROptions opts;
3opts.AddLang("fra");
4opts.AddLang("spa");

Output quality options

When processing documents with known layouts, we can enhance output quality by either specifying regions that we want OCR to ignore via OCROptions::AddIgnoreZonesForPage, or listing exclusive regions to process via OCROptions::AddTextZonesForPage. Both zone options act as stencils: With the ignore zones, we white out the areas inside the supplied rectangular regions before processing, and, for the text zones, we white out areas outside of the supplied regions. The options store an array of RectCollection, where the index into the array corresponds to the relevant page number. OCROptions::AddIgnoreZonesForPage can also be used to skip pages via setting ignore zone to equal page's media box.

1// Optionally specify page zones for OCR extraction in a multipage document
2RectCollection page_zones;
3
4page_zones.AddRect(900, 2384, 1236, 2480);
5page_zones.AddRect(948, 1288, 1672, 1476);
6
7// OCR will only process the two specified zones on the first page
8opts.AddTextZonesForPage(page_zones, 1);
9
10// Reset zone container
11page_zones.Clear();
12
13page_zones.AddRect(428, 1484, 1784, 2344);
14
15// OCR will only process one specified zone on the second page
16opts.AddTextZonesForPage(page_zones, 2);

Setting Input Resolution

We enable users to manually set input image resolution (tweaking which can often lead to better results in practice).

1// Manually override DPI
2opts.AddDPI(300);

Setting the Threading Mode

This is only available with the default OCR.

The default OCR Module contains a manifest file (Lib/OCRModuleApryse.manifest) that lists the engine's capabilities in JSON format. This is the place where threading mode may be configured.

Open the manifest file in a text editor, such as Notepad, and find the line "threading_mode": "". Edit the empty value based on your preference:

  • Minimum threads: "threading_mode": "limited". Use as few threads as possible. This is ideal in a server environment, where each web requests serves a different user, so each OCR process should limit its threads usage.
  • Balanced threads: "threading_mode": "optimized". Use more threads to ensure the OCR process finishes as fast as possible. This is ideal in a desktop or single-user batch environment, where processing time is critical, and the OCR is allowed to use the optimal number of threads.
  • System default: "threading_mode": "". The engine chooses the ideal value based on the environment. This setting is used out of the box.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales