SearchAndReplaceTextInContent

Extraction, Unicode, content editing

Description

Replaces every non-overlapping query occurrence in supported page text, using exact source byte ranges and a verified font mapping

The method preserves surrounding text, existing TJ numeric adjustments and the original following text anchor; an empty replacement deletes only the matched characters

All selected hits are planned and rewritten before page content is committed; unrepresentable text, incomplete font mappings or an unsupported source location reject the request without replacing any selected page text

Verified ToUnicode inheritance resolves embedded base streams and the five bundled Adobe ToUnicode names, honouring child overrides and a shared inheritance budget. The Unicode mapping references in use, inherited stream bytes and simple-font encoding sources are all rechecked before commit. Unknown names, cycles and invalid byte ranges still fail explicitly; see Unicode CMap Resources

Syntax

Delphi

Function TPDFlib.SearchAndReplaceTextInContent(Const Query, Replacement: WideString; Options: TPDFlibSearchOptions; Const PageRange: WideString): Integer;

Parameters

QueryText to find; it must be nonempty and is matched with the options and the font mappings described here
ReplacementReplacement text; it may be empty, which deletes the matched characters, and it keeps the verified original font because this entry delegates to SearchAndReplaceTextInContentEx with ReplacementFontID 0
OptionsTPDFlibSearchOptions set carrying the case, whole-word, wildcard, diacritic, canonical-equivalence, grapheme and soAcrossLines flags; a call during an open API scope is rejected
PageRangePage range in the existing range syntax; an empty string selects every page

Return values

Returns the number of replaced matches, or 0 for no matches or a rejected request; inspect GetLastUnicodeReplacementDiagnostics to distinguish those outcomes

LastErrorCode 535 reports invalid input or capture limits; 537 reports unsupported editing or encoding; a valid search with no matches reports completed with replacedHits 0

Remarks

Composite horizontal metrics keep the real DW and W widths, and the staged replacement rechecks its Encoding, horizontal-width and used vertical-metric sources before commit; see CID Font Widths

Type3 replacement verifies Widths, FontMatrix, a complete Differences encoding and the first d0 or d1 in every glyph procedure it uses. ToUnicode mappings and complete known glyph-name mappings are both supported; unknown private names need explicit Unicode mappings. See Type3 exact editing for geometry, limits and failure behaviour

Exact replacement supports Identity-V composite fonts that have verified ToUnicode source mappings and valid DW2 and W2 metrics. Further vertical Encoding CMaps are supported when an embedded TrueType program plus CIDToGIDMap, or an embedded CFF1 program, supplies verified glyphs for everything used; see Composite Font Glyph Sources. Vertical adjustments keep the original following-text position intact, taking account of real-valued metrics, existing TJ offsets, character spacing, text rise and non-default horizontal scaling

A replacement font must share the original font's writing mode. Any other vertical Encoding CMap stays unsupported until its writing direction and code-to-CID correspondence have been verified; a CMap name that merely looks vertical does not authorise exact editing

Text in nested Forms is supported when the search captures a verified chain of Do invocations and decoded source snapshots. Only the selected invocations are copied, together with their ancestor Forms and resource dictionaries; other calls and pages keep their original streams

SetTextExtractionArea can pick out a single visible invocation of a shared Form. State may carry across page content layers provided every operator has an unambiguous source location

Inherited selected fonts keep their original resource scope even when a nested Form defines a font of the same name. Private Form copies that lack Resources are given explicit page resources, as ISO 32000-2 section 7.8.3 requires for legacy resource fallback; viewers that inherit the parent Form's resources instead may render the original differently

A tagged Form carrying StructParent or StructParents is edited only when it, and every Form enclosing it, is shown exactly once through resources that have a single owner; the copy takes over the structure tree's MCR /Stm and OBJR /Obj references and the original is no longer shown. A tagged Form that other pages also show is edited when this page shows it, and every Form enclosing it, once along its invocation path and every structure reference names its page via /Pg: the copy takes over the edited page's references under a new parent tree key that duplicates the original entry, whilst the other pages keep the original. Tagged Forms shown more than once on a single page, annotation appearances and nested text without a verified Do chain remain unsupported. Reference XObjects (Form XObjects with /Ref) are always rejected, because reference-aware viewers display the target page rather than the proxy stream; the error states whether the target sits in an embedded file or an external document; this operation never guesses structure-tree ownership or semantic mappings

Text inside marked content whose property list carries ActualText, or whose MCID belongs to a structure element with ActualText of its own or inherited from an ancestor, is left unreplaced, since that ActualText states what the content reads as and would no longer describe the substituted glyphs. The same applies to a Form shown inside such marked content

Mappings come from a fully supported ToUnicode CMap or a verified simple-font encoding, including supported Differences glyph names; no system-code-page conversion or guessed glyph encoding is used

Unknown UseCMap names, unresolved mappings, unverified Type3 metrics, unverified vertical encodings, synthetic characters, ActualText replacements, transcoded strings, cross-layer token spans and partial ligature clusters are reported as unsupported

A complete ligature cluster can be replaced when its original bytes and Unicode mapping are verified; prefixes and suffixes remain in their original font

Search options keep their existing meaning, including case, whole-word, wildcard, diacritic, canonical-equivalence and grapheme constraints; soAcrossLines additionally supports verified matches spanning extracted text blocks, whereas calls made during an open API scope are rejected

Across-line replacement plans every non-overlapping match using the existing joined-block search rules; synthetic search separators carry no encoded glyph bytes and are never inserted into the source

The replacement starts at the first original matched anchor; matched glyphs in later blocks are removed but their original advances are preserved, so untouched following text stays where it was and no automatic text reflow takes place

Every source span crossed must pass the same complete-glyph, font-encoding and invocation checks; an unsupported span further on rejects the whole selected replacement request, earlier valid hits included

Ordinary SearchText across-line result fields and first-hit behaviour are unchanged

Planning supports up to 4096 matches; captured source snapshots are capped at 64 MiB per stream and 512 MiB in total, with 64 MiB of captured source-location records; Type3 glyph programs in use, composite font-program and glyph-index sources, and retained Unicode mapping source snapshots all draw on the transaction source budget; nested Form chains are limited to 64 levels and planning to 16384 page and Form scopes; the font mapping parser also applies bounded token and code-space limits, with transaction totals of 262144 mappings and 8 Mi UTF-16 target units

These limits constrain retained capture and planning data; existing page loading and rendering have their own allocations, so they are no guarantee of absolute peak memory usage

Shared content streams and resource dictionaries are copied for the selected pages and Form invocations; document selection is restored and stale text-block lists must be extracted again after a successful edit

This operation changes active page content and is not a secure-erasure API: detached objects or earlier incremental revisions can retain original bytes

See also

SearchAndReplaceTextInContentEx, GetLastUnicodeReplacementDiagnostics, SearchText