PDFium Delphi Component Docs

CreateDocumentSearch method

Component: TPdf  ·  Unit: PDFium
Creates a lazy document-wide text search that returns one native PDFium match per Next call

Syntax

function CreateDocumentSearch(const Text: WString; Options: TSearchOptions = []; StartPage: Integer = 0; Direction: TPdfDocumentSearchDirection = dsdForward; const CancellationToken: IPdfCancellationToken = nil): TPdfDocumentSearch;

Parameters

TextNon-empty Unicode text without embedded null characters
OptionsNative PDFium case-sensitive, whole-word, and consecutive-match options
StartPageOne-based first page, or 0 for page 1 when searching forwards and the final page when searching backwards
DirectiondsdForward or dsdBackward
CancellationTokenOptional cooperative cancellation token checked at page boundaries and before each match is published

Return Value

A caller-owned TPdfDocumentSearch instance

Description

The search opens only its current page, text page, and native search handle, releasing them before it advances to the next page, so native handle memory does not grow with document length

Next returns the page number, native character range, matched text, and all page-space rectangles in one result record, avoiding a page switch or a second page parse for highlighting

An empty active document produces an already finished search. Closing the owner document, or changing its page tree with AddPage, DeletePage, ImportPages, ImportPagesByIndex or MovePages, safely finishes every outstanding search; create a new search afterwards. Do not edit page content whilst a search is being advanced

Example

var
  Search: TPdfDocumentSearch;
  Match: TPdfDocumentSearchResult;
begin
  Search := Pdf1.CreateDocumentSearch('invoice');
  try
    while Search.Next(Match) do
      Memo1.Lines.Add(Format('Page %d, character %d', [Match.PageNumber, Match.StartIndex]));
  finally
    Search.Free;
  end;
end;

See Also

TPdfDocumentSearch, FindFirst, RectangleCount