SearchAndReflowTextInContent
Extraction, Unicode, content editing
Description
Replaces every supported non-overlapping query match with independently wrapped text in an explicit box, using verified source glyphs, font mappings and horizontal advances
Each box starts at the first matched text anchor, retains its original text-matrix direction, transformation, text rise and paint style, and wraps downward in that local text coordinate system; all original matched glyphs are removed and untouched following text keeps its original position
All boxes, fonts, byte edits and invocation-specific Form copies are prepared before any selected page is changed; one rejected match retains every selected original source
Syntax
Delphi
Function TPDFlib.SearchAndReflowTextInContent(Const Query, Replacement: WideString;
Options: TPDFlibSearchOptions; Const PageRange: WideString;
BoxWidth, BoxHeight, FontSize, LineHeight, MinFontSize: Double;
ReplacementFontID, FallbackFontID, OverflowPolicy: Integer): Integer;
DLL
int DLSearchAndReflowTextInContent(int InstanceID, const wchar_t* Query,
const wchar_t* Replacement, int Options, const wchar_t* PageRange,
double BoxWidth, double BoxHeight, double FontSize, double LineHeight,
double MinFontSize, int ReplacementFontID, int FallbackFontID, int OverflowPolicy);
ActiveX
Function PDFlib::SearchAndReflowTextInContent(Query As String, Replacement As String,
Options As Long, PageRange As String, BoxWidth As Double, BoxHeight As Double,
FontSize As Double, LineHeight As Double, MinFontSize As Double,
ReplacementFontID As Long, FallbackFontID As Long, OverflowPolicy As Long) As Long
Parameters
| Query, Options, PageRange | Use the search and exact-source qualification contract of SearchAndReplaceTextInContentEx, including soAcrossLines; an empty range selects all pages |
|---|---|
| Replacement | Valid UTF-16 with at most 65536 units; CR, LF and CRLF request paragraph breaks, and an empty string deletes the verified match |
| BoxWidth, BoxHeight | Positive finite values through 1000000 in local text-matrix units, before the original text matrix and graphics CTM; width constrains verified advances and height constrains one nominal font em plus the baseline spacing of subsequent lines |
| FontSize | 0 keeps the first matched original font size; otherwise a positive finite value through 1000000 |
| LineHeight | 0 uses 1.2 times the selected size; an explicit value must be at least that size; shrinking scales both the font size and the line height by the same ratio |
| MinFontSize | 0 uses the smaller of 4 and the selected size; an explicit positive value must not exceed the selected size |
| ReplacementFontID | 0 reuses the first matched original font; a positive document font ID selects one verified font for the replacement box |
| FallbackFontID | 0 disables fallback; a positive document font ID is tried for the entire box only when the selected font cannot uniquely encode the replacement paragraphs; no system font is selected or installed automatically |
| OverflowPolicy | 0: PDF_TEXT_REFLOW_REJECT, reject the complete request if any box overflows; 1: PDF_TEXT_REFLOW_TRUNCATE, emit only whole fitting lines and report the remaining input; 2: PDF_TEXT_REFLOW_SHRINK, find a fitting size within the requested minimum and selected size, or reject the complete request |
Return values
Returns the number of query matches that were replaced and reflowed. Returns 0 when no match was found or the input is invalid.
Layout and source guarantees
Wrapping prefers the last fitting ordinary space and otherwise breaks at a complete verified font-code and Unicode grapheme boundary; encoded ligatures and surrogate pairs are never split, and spaces are retained in the emitted line strings
The replacement uses its selected font and size with zero character and word spacing and 100 percent horizontal scaling; original color, transparency, rendering mode and text rise are retained, and all original text-state values, the text line matrix and the following text position are restored
Box dimensions describe nominal line layout rather than tight glyph ink bounds; glyph outlines can overhang their advances or font em, and original page, Form and other clipping paths still apply
The caller allocates the box and reviews overlaps, page bounds and existing clipping; the operation preserves paint order and surrounding backgrounds without painting a covering rectangle or moving neighboring paragraphs
The finite profile requires horizontal nonnegative advances, a nonsingular restorable text matrix, resolved preceding text advances and nonclipping text rendering modes 0 through 3; vertical writing, pending text clips, ActualText and unverified or ambiguous source mappings reject the request
Selected nested and shared Form invocations are copied with their verified resource scope; other calls and pages retain their original streams, following the exact-edit structure ownership and source-retention rules
All replacement paragraphs must encode uniquely even when truncation will omit later text; fallback is a whole-box encoding choice and does not repair invalid original sources, unsupported metrics, overflow or malformed font mappings
The operation performs font-code wrapping without new shaping, bidirectional reordering, hyphenation, style-run inference or semantic certification; use a suitable explicitly prepared font and review complex script appearance
Results and limits
Returns the number of replaced matches, or 0 for no matches or a rejected request; GetLastUnicodeReplacementDiagnostics distinguishes those outcomes
Successful edits report engine box-reflow and a boxes array with hit, lines, fontSize, remainingStart, remainingUTF16Units and usedFallback for each match; remainingStart is a one-based UTF-16 index into Replacement, and equals its length plus one when nothing remains
Policy 1 consumes the original complete match even if no replacement line fits; the caller uses the reported remaining input for continuation, and a trailing newline that requires another empty line remains pending when that line does not fit
Invalid scalar parameters or font IDs set LastErrorCode 535; source, encoding, layout, overflow and planning-limit failures set 537 and retain the original selected page text; no matches reports completed with replacedHits 0
Existing exact-edit source and capture budgets apply, with at most 4096 matches, 65536 replacement clusters and 4096 output lines per box, 4000000 aggregate layout work steps per call, and 24 bounded shrink refinements; retained reflow plans share the 512 MiB transaction budget
Open authoring scopes reject the operation; successful edits restore document selection and invalidate previously extracted text-block snapshots; detached objects and earlier incremental revisions can retain the original bytes, so this is not a secure-erasure API
Native and Automation interfaces
The DLL uses stdcall; macOS Dylib uses cdecl and the same arguments, with wide and A variants and matching diagnostic getters; A inputs use the instance ANSI-to-wide conversion configuration and diagnostic A getters return UTF-8
ActiveX exposes the method through IPDFlibrary18 and default IDispatch; CoPDFlibrary.CreateTextReflowTools returns the typed extension, while all earlier interface IIDs and method order remain available
Native integer Options use bits 1 through 128 in search enum order, including bit 16 for soAcrossLines; unknown bits and invalid native instances return 0
Example
Count := PDF.SearchAndReflowTextInContent('old paragraph', NewText,
[soAcrossLines], '1', 220, 90, 0, 0, 8, 0, FallbackFontID,
PDF_TEXT_REFLOW_SHRINK);
Report := PDF.GetLastUnicodeReplacementDiagnostics;
See also
SearchAndReplaceTextInContentEx, SearchAndReflowTextInContentA, GetLastUnicodeReplacementDiagnostics, AutomationExtensions