PDFium Delphi Component Docs

Optional Tesseract OCR backend

Requires the Small Business or Enterprise edition: the unit ships in Lib\SmallBusiness

CreatePdfTesseractOcrProvider supplies ApplyOcrSearchLayer with an IPdfOcrProvider; installing Tesseract and traineddata is optional and external

Specify an absolute library path, tessdata directory and language identifiers such as eng; the DLL must match the process architecture

If dependencies fail, TryCreatePdfTesseractOcrProvider returns details in ErrorMessage with a nil Provider, without altering the process search path

Each recognition call owns a separate native API instance, composites BGRA alpha onto white, decodes UTF-8 text and returns word quadrilaterals in top-left image coordinates with confidence normalised to 0–1

Since v3.257.0, the returned provider also implements IPdfStructuredOcrProvider. With UseNativeLayout=True, the default, native iterator boundaries populate lines, paragraph blocks and physical regions in engine reading order. ApplyPdfStructuredOcr uses this interface automatically; word-only RecognizePage calls retain their existing result shape

TPdfOcrPageLayout.Regions partitions paragraph Blocks through FirstBlock and BlockCount. Native layout uses Source=polsProvider; setting UseNativeLayout=False selects the existing word adaptation with Source=polsHeuristic and no regions. Libraries lacking the optional boundary export also fall back explicitly and return a diagnostic

The engine's physical and paragraph segmentation does not automatically establish meaningful heading levels, reviewed table cells or recognition accuracy. The recorded English paragraph/column profiles compare every boundary and word box with the separate native hOCR output, retain scan pixels/components and survive dirty native saves

EPdfTesseractError is raised for a missing library or export, options outside their finite limits, absent tessdata or language models, failed recognition, and exhausted time or output budgets

TPdfTesseractOptions bounds provider creation and every recognition call; values outside the documented ranges fail fast with EPdfTesseractError rather than reaching the native engine

Default sets page-segmentation mode 3 and engine mode 3, a 60,000 ms deadline, and allowances of 16,777,216 pixels, 100,000 words and 1,048,576 text characters

PageSegmentationMode accepts Tesseract page-segmentation ordinals 3 through 13 and forwards the mode to the engine before recognition

EngineMode accepts Tesseract engine-mode ordinals 0 through 3 and selects how the language models are initialised

TimeoutMilliseconds accepts 1 through 3,600,000 ms per recognition call and is enforced by the native monitor, its cancel callback and explicit phase checks

MaxPixels caps the recognised BGRA image area, 16,777,216 pixels by default and 67,108,864 at most

MaxWords caps the words one call can return, 100,000 by default and 1,000,000 at most

MaxTextCharacters caps the native UTF-8 text scan and the total characters across returned words, 1,048,576 by default and 16,777,216 at most

Recognition checks cancellation and its deadline; native model initialisation has no cancellation callback, so the allowance is checked just before and after initialisation

Failed calls throw away partial output, and cancellation retains EPdfOperationCancelled; incomplete searchable text is never added silently

Historical word-provider ABI fixtures cover Delphi and FPC Win32/Win64. The v3.257.0 native-layout profile uses actual Tesseract 5.5.3 on Win64 in both compilers, including separate hOCR hierarchy checks, searchable-layer save/reopen, unchanged rendering and explicit fallback controls; other native-layout targets remain unqualified

ProviderOptions := TPdfTesseractOptions.Default;
Provider := CreatePdfTesseractOcrProvider(
  LibraryPath, TessdataDirectory, 'eng', ProviderOptions);
OcrOptions := TPdfOcrOptions.Default;
Pdf.ApplyOcrSearchLayer(Provider, OcrOptions, Report, CancellationToken);

Public declarations

type
  EPdfTesseractError = class(Exception);

  TPdfTesseractOptions = record
    // Tesseract PSM ordinals: 3 = automatic, 6 = block, 7 = single line
    PageSegmentationMode: Integer;
    // Tesseract OEM ordinals: 1 = LSTM, 3 = default
    EngineMode: Integer;
    TimeoutMilliseconds: Cardinal;
    MaxPixels: Int64;
    MaxWords: Integer;
    MaxTextCharacters: Integer;
    UseNativeLayout: Boolean;
    class function Default: TPdfTesseractOptions; static;
  end;

// Each recognition call owns a separate API instance; providers can be shared
// Libraries and traineddata remain optional, explicitly configured dependencies
function CreatePdfTesseractOcrProvider(const LibraryPath, TessdataDirectory,
  Languages: string; const Options: TPdfTesseractOptions): IPdfOcrProvider;

// Dependency errors are returned without changing the process search path
function TryCreatePdfTesseractOcrProvider(const LibraryPath, TessdataDirectory,
  Languages: string; const Options: TPdfTesseractOptions;
  out Provider: IPdfOcrProvider; out ErrorMessage: string): Boolean;