Skip to content

OCR (Optical Character Recognition)

OCR recognizes text in scanned documents, image-based PDFs, and mixed PDFs so it becomes searchable or editable. This topic covers typical use cases, prerequisites, main APIs, and basic integration.

To set up a resource directory and run the bundled sample quickly, see OCR sample setup and run guide. To run OCR from the command line, see OCR command-line tool.

Use cases

OCR is suited for:

  • Text recognition in scans and image-based PDFs
  • Archiving and retrieval that require searchable or editable PDFs
  • Exporting recognized content to Word, Excel, PowerPoint, HTML, and other formats
  • Server-side batch, offline, or multiprocess processing

System requirements and prerequisites

  • Platforms: Windows, Linux x64
  • License: authorization that includes the OCR module
  • Resources: OCR add-on resource package or OCR runtime resource directory

How to obtain OCR resources, directory layout, and sample steps are described in OCR sample setup and run guide. Prepare the resource directory before using OCR APIs or the OCR command-line tool.

Main APIs

ClassPurpose
OCREngineInitialize and release the OCR engine; set language, logging, and OCR callbacks
OCRRun OCR on a page, document, batch, or convert output format after OCR
OCRConfigOCR options such as picture detection, denoising, deskew, resolution, and confidence
OCRCallbackCancel callbacks; use IsImageIgnored() to skip specific image objects
OCRProgressCallbackOCR progress notifications
OCRSettingDataDocument, page range, output type, and config for OCRPDFDocuments()

Typical integration flow

  1. Initialize Foxit PDF SDK
  2. Prepare the OCR resource directory and initialize OCREngine
  3. Set languages, for example English or multiple languages
  4. Configure OCRConfig for your scenario
  5. Call OCRPDFPage, OCRPDFDocument, or OCRConvertTo
  6. Save the processed document or conversion output

A full runnable sample is in /examples/simple_demo/ocr/ocr.cpp in the SDK package. The snippet below matches that sample; license initialization, input/output paths, and other boilerplate are omitted:

c++
#include "include/addon/ocr/fs_ocr.h"

using namespace foxit;
using namespace foxit::addon::ocr;

void RunOCR(const wchar_t* input_file,
            const wchar_t* output_file,
            const wchar_t* ocr_resource_path) {
  // Foxit PDF SDK should have been initialized before OCR starts.
  OCREngine::Initialize(ocr_resource_path);
  OCREngine::SetLanguages(L"English");

  PDFDoc doc(input_file);
  if (doc.Load() != foxit::e_ErrSuccess) {
    return;
  }

  OCRConfig config;
  config.is_detect_pictures = true;
  config.is_remove_noise = true;
  config.is_correct_skew = true;
  config.is_auto_overwrite_resolution = false;
  config.resolution_to_overwrite = 300;
  config.confidence = 30;

  OCR ocr;
  ocr.OCRPDFDocument(doc, false, config);
  doc.SaveAs(output_file, PDFDoc::e_SaveFlagNoOriginal);

  OCREngine::Release();
}

Output types

OCR produces mainly:

  • Searchable PDF: preserves visual appearance and adds a searchable text layer
  • Editable PDF: writes recognition results into document content for further editing

Use OCRConvertTo to export to Office or HTML after OCR.

Configuration

Common OCRConfig options:

  • Picture detection: is_detect_pictures
  • Preprocessing: is_remove_noise, is_correct_skew
  • Recognition mode: is_enable_text_extraction_mode
  • Processing order: is_sequentially_process for sequential vs automatic parallel page processing
  • Resolution: is_auto_overwrite_resolution, resolution_to_overwrite
  • Filtering: confidence threshold

Typical guidance:

  • Noisy scans: keep is_remove_noise = true
  • Skewed pages: keep is_correct_skew = true
  • Maximize text capture: consider is_enable_text_extraction_mode
  • Strict quality: raise confidence
  • Wrong image resolution metadata: set is_auto_overwrite_resolution to false and set resolution_to_overwrite
  • Batch multi-document jobs: consider OCRPDFDocuments

For single-process tasks, use the default OCREngine initialization. For server batch or multiprocess workloads, consider OCREngine::Initialize(ocr_resource_path, true) to enable shared CPU cores mode.

OCR API vs OCR command-line tool

Choose by integration style:

  • Business workflows, orchestration, or in-app UI: use the OCR API
  • Quick validation, batch scripts, or external process invocation: use the OCR command-line tool

To verify resources, license, and environment first, start with OCR sample setup and run guide.