PDFium Delphi Component Docs

FindFirst method

Component: TPdf  ·  Unit: PDFium
Start a search of the specified text. StartIndex is 0-based, -1 is for end of the page. DirectionUp parameter determines direction of this search operation

Syntax

function FindFirst(const Text: WString; Options: TSearchOptions = []; StartIndex: Integer = 0; DirectionUp: Boolean = True): Integer;

TextWString. The Unicode text string to search for on the current page
OptionsTSearchOptions. A set of TSearchOption flags that control matching behaviour. Supported flags are seCaseSensitive (case-sensitive match), seWholeWord (whole-word match only) and seConsecutive. Pass [] for a case-insensitive, substring search
StartIndexInteger. The 0-based character index at which the search begins. Pass -1 to start from the last character on the page (useful when DirectionUp is False). Defaults to 0
DirectionUpBoolean. Determines the initial search direction. True (default) searches from StartIndex towards the end of the page; False searches towards the beginning. Subsequent calls to FindNext always move forward; FindPrevious always moves backward.

Return Value

The 0-based character index of the first match, or -1 if no match is found. The internal search handle is retained so that subsequent calls to FindNext or FindPrevious can continue the search without reinitialisation

Description

FindFirst initiates a full-text search on the currently loaded PDF page. It accepts a Unicode search string together with optional matching flags, a starting character position, and a direction hint. On success it returns the 0-based character index of the first match and stores an internal search handle that is reused by subsequent FindNext and FindPrevious calls. Returns -1 when the text is not found

The StartIndex parameter lets you begin the search at any character position rather than always from the top of the page. Pass 0 (the default) to start from the very first character, or pass -1 to anchor at the last character — convenient when performing a reverse search with DirectionUp = False

Use the Options parameter to refine matching behaviour. seCaseSensitive enables a case-sensitive comparison; seWholeWord restricts matches to whole words only; seConsecutive enables consecutive matching. Flags may be combined. Passing an empty set [] performs a case-insensitive substring search

Once a match is found, call RectangleCount with the returned index and the known match length to retrieve the bounding rectangles of the matched text on the page — useful for highlighting search results in a viewer

Example

var
  Idx, MatchLen, R, I: Integer;
  Rect: TPdfRectangle;
begin
  Pdf1.PageNumber := 1;
  MatchLen := Length('invoice');
  Idx := Pdf1.FindFirst('invoice', [], 0, True);
  while Idx >= 0 do
  begin
    // Retrieve bounding rectangles for the matched text
    R := Pdf1.RectangleCount(Idx, MatchLen);
    for I := 0 to R - 1 do
    begin
      Rect := Pdf1.Rectangle[I];
      Memo1.Lines.Add(Format('Match at char %d, rect [%.1f,%.1f,%.1f,%.1f]',
        [Idx, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom]));
    end;
    Idx := Pdf1.FindNext;
  end;
end;

See Also

FindNext, FindPrevious, Find, CharacterIndexAtPos, CharacterCount, RectangleCount, Text