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, with child overrides and shared inheritance budgets. Used Unicode mapping references, inherited stream bytes, and simple-font encoding sources are rechecked before commit. Unknown names, cycles, and invalid byte ranges remain explicit failures; 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 retain real DW and W widths, and 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 each used glyph procedure. Both ToUnicode mappings and complete known glyph-name mappings are supported; unknown private names require explicit Unicode mappings. See Type3 exact editing for geometry, limits, and failure behavior

Exact replacement supports Identity-V composite fonts with verified ToUnicode source mappings and valid DW2 and W2 metrics. Additional vertical Encoding CMaps are supported when an embedded TrueType program and CIDToGIDMap, or an embedded CFF1 program, provide verified used glyphs; see Composite Font Glyph Sources. Vertical adjustments preserve the original following-text position, including real-valued metrics, existing TJ offsets, character spacing, text rise, and non-default horizontal scaling

A replacement font must use the same writing mode as the original font. Other vertical Encoding CMaps remain unsupported until their writing direction and code-to-CID correspondence are verified; a vertical-looking CMap name does not authorize exact editing

Nested Form text is supported when the search captures a verified chain of Do invocations and decoded source snapshots. Only selected invocations are copied, including their ancestor Forms and resource dictionaries; other calls and pages retain their original streams

SetTextExtractionArea can select one visible invocation of a shared Form. State can continue across page content layers when each operator has an unambiguous source location

Inherited selected fonts retain their original resource scope even when a nested Form defines the same font name. Private Form copies without Resources receive explicit page resources, as required for legacy resource fallback by ISO 32000-2 section 7.8.3; viewers that instead inherit parent Form resources may render the original differently

A tagged Form with StructParent or StructParents is edited only when it and every enclosing Form are shown exactly once through resources with a single owner; the copy takes over the MCR /Stm and OBJR /Obj references of the structure tree and the original is no longer shown. A tagged Form that other pages also show is edited when the page shows it, and every Form enclosing it, once along its invocation path and every structure reference names its page through /Pg: the copy takes the references of the edited page under a new parent tree key that repeats the original entry, and the other pages keep the original. Tagged Forms shown more than once on one 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 show the target page instead of the proxy stream; the error names whether the target lives in an embedded file or an external document; this operation does not guess structure-tree ownership or semantic mappings

Text inside marked content whose property list has ActualText, or whose MCID belongs to a structure element that has ActualText itself or through an ancestor, is not replaced, because that ActualText states what the content reads as and would no longer describe the replaced glyphs. This also 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 retain their existing meaning, including case, whole-word, wildcard, diacritic, canonical-equivalence and grapheme constraints; soAcrossLines also supports verified matches across extracted text blocks, while calls 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 have no encoded glyph bytes and are not inserted into the source

The replacement begins at the first original matched anchor; matched glyphs in later blocks are removed with their original advances preserved, so untouched following text keeps its position and no automatic text reflow occurs

Each crossed source span must pass the same complete-glyph, font-encoding and invocation checks; a later unsupported span rejects the entire selected replacement request, including earlier valid hits

Ordinary SearchText across-line result fields and first-hit behavior remain unchanged

Planning supports up to 4096 matches; captured source snapshots are limited to 64 MiB per stream and 512 MiB in total, with 64 MiB of captured source-location records; used Type3 glyph programs, composite font-program and glyph-index sources, and retained Unicode mapping source snapshots share 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 not a guarantee of absolute peak memory usage

Shared content streams and resource dictionaries are copied for 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