THotPDF.PreviewLoadedPageTextReplacement Method
Prepares replacement text with embedded Unicode fonts, measures each match, and returns an auditable plan without changing the loaded document
function PreviewLoadedPageTextReplacement(PageIndex: Integer;
const Needle, Replacement: UnicodeString; MatchCase: Boolean;
const Options: THPDFLoadedTextReplacementOptions;
out Plan: THPDFLoadedTextReplacementPlan): Boolean;
Options and results
THPDFLoadedTextReplacementOptions.Default uses Arial, the match's original font size, its measured width, and a height of 1.25 times that size, with atomic rejection enabled
Appearance selects the font, style, colour, alignment and padding; UseMatchFontSize=False uses its requested size, whilst positive BoxWidth and BoxHeight provide an explicit wrapping box
Appearance.OverflowPolicy selects ltopReject, ltopClip, or ltopShrinkToFit; clipping retains only the producer's fitting text and shrink-to-fit stops at MinFontSize
THPDFLoadedTextReplacementPlan carries the request, options, source fingerprint, counts, and THPDFLoadedTextReplacementResults array
Each THPDFLoadedTextReplacementResult reports glyph indexes, original position, box dimensions, effective font size, and a THPDFLoadedTextReplacementStatus value: ltrsReady, ltrsApplied, ltrsRejected, or ltrsRolledBack, with a reason for rejection or rollback
MaxMatches, MaxGlyphs, MaxContentTokens, MaxContentBytes, MaxGeneratedBytes and the appearance text limit bound source parsing, output and copied font resources
Geometry and atomicity
The generated replacement uses the document's font fallback chains, ShapingFeatures, script auto-shaping switches, and GSUB script and language selections; configure these before previewing and keep the same settings when applying the plan
Each replacement appearance limits distinct shaping font selections to 256 and cumulative raw font data to 64 MiB, in addition to the request's parsing and generated-output budgets
Replacement switches fonts within the original text stream, keeps surrounding text searchable, and restores the original pen and line matrices so following glyphs and line transitions keep their positions
Direct page text using horizontal, nonsingular geometry is supported, including page transforms, text scaling, rise, TJ, single/double-quote operators and matches crossing adjacent operands on the same baseline
Matches crossing baselines, vertical writing, singular transforms, unbalanced content, missing glyphs, and documents containing signature fields are rejected; text inside Form XObjects is outside this API's match scope
With Atomic=True, any rejected match rolls back all ready matches; with Atomic=False, ready matches may be applied whilst rejected matches remain unchanged, and the Boolean result remains False when any match was rejected
Preview uses and rolls back a nested graph transaction, preserving an existing caller transaction; it can invalidate document caches and invoke the document-modification notification even though content is restored
Commit
Pass the plan to ApplyLoadedPageTextReplacement to revalidate its content and glyph-metric fingerprint before applying the request in a graph transaction
Recompute a plan after content changes; a stale plan raises EInvalidOpException without applying its replacements
Options := THPDFLoadedTextReplacementOptions.Default;
Options.Appearance.OverflowPolicy := ltopShrinkToFit;
if PDF.PreviewLoadedPageTextReplacement(0, 'Original', NewText,
True, Options, Plan) then
if PDF.ApplyLoadedPageTextReplacement(Plan, Applied) then
PDF.SaveLoadedDocument(TargetPath);