SearchAndReplaceTextInContentEx

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.SearchAndReplaceTextInContentEx(Const Query, Replacement: WideString; Options: TPDFlibSearchOptions; Const PageRange: WideString; ReplacementFontID: Integer): Integer;

DLL

int DLSearchAndReplaceTextInContentEx(int InstanceID, const wchar_t* Query, const wchar_t* Replacement, int Options, const wchar_t* PageRange, int ReplacementFontID);
int DLSearchAndReplaceTextInContentExA(int InstanceID, const char* Query, const char* Replacement, int Options, const char* PageRange, int ReplacementFontID);

ActiveX

Function PDFlib::SearchAndReplaceTextInContentEx(Query As String, Replacement As String, Options As Long, PageRange As String, ReplacementFontID As Long) As Long

Parameters

QueryText to find; it must be nonempty. The DLL A entry points convert it from the instance ANSI configuration
ReplacementReplacement text; it may be empty, which deletes the matched characters, and every character must be representable in the replacement font
OptionsTPDFlibSearchOptions set carrying the case, whole-word, wildcard, diacritic, canonical-equivalence, grapheme and soAcrossLines flags; the DLL and ActiveX facades pass these as bits 1, 2, 4, 8, 16, 32, 64 and 128 in enum order, reject unknown bits, and reject a call during an open API scope
PageRangePage range in the existing range syntax; an empty string selects every page. The DLL A entry points convert it from the instance ANSI configuration
ReplacementFontID0 keeps the original font; a positive value must identify a font in the selected document with a complete verified encoding, is used only for replacement runs, and the original font is restored before untouched suffixes

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

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

Simple fonts must supply valid PDF Widths, FirstChar and LastChar, or use an exact standard fourteen Type1 font name without Widths; system font fallback and loose name matching do not authorize exact advance

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

Use SearchAndReflowTextInContent for an explicitly sized replacement box with wrapping, selected style, controlled fallback and overflow handling

See also

SearchAndReplaceTextInContent, GetLastUnicodeReplacementDiagnostics, SearchText