PDFium Delphi Component Docs

Optional Tesseract OCR provider

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

CreatePdfTesseractOcrProvider supplies an IPdfOcrProvider for ApplyOcrSearchLayer; Tesseract and traineddata are optional external dependencies

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

TryCreatePdfTesseractOcrProvider returns dependency failures in ErrorMessage and leaves Provider nil without changing 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 normalized 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 instead of reaching the native engine

Default selects page segmentation mode 3 and engine mode 3, a 60,000 ms deadline, 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 initialized

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 recognized 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 monitors cancellation and the deadline; model initialization has no native cancellation callback, so the budget is checked immediately before and after initialization

Failed calls discard partial output and cancellation preserves EPdfOperationCancelled; the API does not silently add incomplete searchable text

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;