ApplyOCRTextLayer

OCR, text, document editing

Description

Validates provider-neutral OCR JSON and adds fitted rendering-mode-3 text to a page

The added text is searchable and selectable but invisible when the page is rendered

Syntax

Delphi

Function TPDFlib.ApplyOCRTextLayer(Page: Integer; Const ResultJSON,
  FontName: WideString; MinConfidence: Double): Integer;

ActiveX

Function PDFlib::ApplyOCRTextLayer(Page As Long, ResultJSON As String, FontName As String, MinConfidence As Double) As Long

DLL

int DLApplyOCRTextLayer(int InstanceID, int Page, const wchar_t* ResultJSON,
  const wchar_t* FontName, double MinConfidence);
int DLApplyOCRTextLayerA(int InstanceID, int Page, const char* ResultJSON,
  const char* FontName, double MinConfidence);

Parameters

PageThe one-based target page
ResultJSONA versioned OCR result object
FontNameAn embeddable TrueType font name or path, or an empty string to select an installed fallback
MinConfidenceThe inclusive confidence threshold from 0 through 1

JSON schema

{
  "version": 1,
  "width": 2480,
  "height": 3508,
  "words": [
    {"text": "Invoice", "left": 120, "top": 180,
     "width": 260, "height": 54, "confidence": 0.98}
  ]
}

left and top use a top-left origin in source units

When both width and height are present, word boxes are scaled from those source dimensions to the PDF page; when both are omitted, coordinates are interpreted as PDF points

Each word requires text, left, top, width, and height; confidence is optional and defaults to 1

Geometry must be finite and non-negative with positive width and height, while confidence must be from 0 through 1

Return values

1The text layer was added, or no word met the threshold
0Validation or writing failed

Remarks

The complete JSON structure and every word boundary are validated before page content is written

Each word is sized from its box height and horizontally fitted with text scaling, while an isolated graphics state prevents OCR formatting from leaking into later drawing operations

LastErrorCode is 110 for invalid OCR JSON and 307 for a page, font, or text-layer writing failure

LastOCRError provides the detailed reason

See also

OCRPage, OCRDocument, LastOCRError, RenderPageToString