SearchText
Text, extraction
Description
Searches text blocks across the requested page range and copies hits into the caller-provided array while returning the total match count even when the array is smaller
A word whose letters change font, and therefore fall into adjacent text blocks that touch on one baseline, is matched as one word. Such a hit belongs to the block where it starts: MatchText runs from that block through the block where the match ends, MatchStart counts from the start of the first block, and the hit rectangle covers all of those blocks. With soAcrossLines no separating space is inserted between such blocks
The method reports page progress through OnProgress, honours Abort between pages and blocks, and restores the page that was selected before searching
Since v3.430.0 TPDFlibSearchOption also accepts soDiacriticInsensitive (1:1 Unicode fold so plain letters match accented Latin, Greek and Cyrillic counterparts), soWildcards (* and ? in the query, leftmost-longest, whole-word compatible) and soAcrossLines (phrases may span line and block breaks through a page-joined match)
Since v3.452.0 soCanonicalEquivalent matches canonically equivalent NFC and NFD text while preserving original hit coordinates, and soGraphemeClusters prevents matches and replacements from splitting surrogate pairs, combining sequences, emoji ZWJ sequences, flags or Indic conjuncts. When combined with soWildcards, one ? consumes one complete grapheme cluster
Hits additionally carry MatchStart and MatchLength positions inside the block text, plus ContextBefore and ContextAfter snippets when SearchContextChars is greater than zero
Use SearchTextEach when hits should be consumed immediately without choosing a result-array capacity
Syntax
Delphi
Function TPDFlib.SearchText(Const Query: WideString; Options: TPDFlibSearchOptions; Const PageRange: WideString; Var Hits: Array Of TPDFlibSearchHit): Integer;
Parameters
Query is the text to look for, and an empty query - or one made up entirely of wildcards when soWildcards is set without soGraphemeClusters - leaves no active query and yields 0. Options is a set drawn from TPDFlibSearchOption: soCaseSensitive, soWholeWord, soDiacriticInsensitive, soWildcards, soAcrossLines, soCanonicalEquivalent and soGraphemeClusters. PageRange is a page-range expression such as 1-3,5, where an empty string means every page, the keywords odd, even, first and last are accepted, bounds may be given in either order, pages outside the document are dropped and any other stray character makes the whole expression select nothing. Hits receives the matches in page and block order and may be declared with any capacity, including zero, in which case the call only counts.
Return values
The total number of matches found, which may exceed Length(Hits) and is therefore the count to size the array with on a second pass. 0 when no document is open, when no query is active, when the page range selects no page, when the search was aborted, or when nothing matched. The internal content-capture path used by ReplaceUnicodeText can also surface 4097 when its capture budget runs out, but an ordinary caller only ever sees hit counts.
Remarks
Searching works on text blocks: matches never cross a block or line boundary unless soAcrossLines is set, in which case the blocks of a page are joined for matching and the hit is attributed to the block that holds the match start. Every occurrence is reported, but the hit rectangle is the box of the whole block containing it rather than the box of the matched run, so combine MatchStart and MatchLength with the character-geometry calls when the glyphs have to be located precisely. A word split by a font change into two adjacent blocks is still one hit, with text and rectangle spanning both blocks. The page selected before the call is restored, Abort takes effect between pages and between blocks, and progress is reported through OnProgress. Only text the extraction engine can see is found, so pages that hold nothing but a scanned image produce no hits until they have been OCRed.