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 LongDLL
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
| Page | The one-based target page |
|---|---|
| ResultJSON | A versioned OCR result object |
| FontName | An embeddable TrueType font name or path, or an empty string to select an installed fallback |
| MinConfidence | The 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 mapped from those source dimensions to the effective rendered page box; when both are omitted, coordinates use raw PDF user-space units in that box
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
| 1 | The text layer was added, or no word met the threshold |
|---|---|
| 0 | Validation or writing failed |
Remarks
This method uses the same owned-layer writer as ApplyOCRTextLayerEx with options zero, preserving the append behavior of the original API
The rendered page box, box offsets, page rotation, and retained content transformation matrix are accounted for when placing invisible text
Optional word fields are baseline with [x1, y1, x2, y2], textAngle, and non-negative integer blockID, paragraphID, lineID, and wordID
A complete baseline supplies the text direction and advance, so it cannot be combined with a nonzero textAngle
Use ApplyOCRTextLayerEx to skip pages with non-owned text or replace exact owned OCR layers, and GetOCRDiagnosticsJSON to inspect the resulting action
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