Skip to main content

TdxPDFDocument.FindText(string,TdxPDFDocumentTextSearchOptions,Integer) Method

Searches for a text string on the specified page of the document.

Declaration

function FindText(const AText: string; const AOptions: TdxPDFDocumentTextSearchOptions; APageIndex: Integer): TdxPDFDocumentTextSearchResult; overload; virtual;

Parameters

Name Type Description
AText string

The searched text string.

AOptions TdxPDFDocumentTextSearchOptions

The required search settings.

APageIndex Integer

The target page’s index.

Returns

Type Description
TdxPDFDocumentTextSearchResult

A text search operation result.

Remarks

Call the FindText function to find the nearest occurrence of a text string according to the specified search settings and the current search operation status.

Example

The following code example searches for the 'Dx' string on the third document page and highlights the results with custom colors:

uses
  dxCoreGraphics; // This unit is required to work with TdxAlphaColor values
// ...
var
  ABackColor, AFrameColor: TdxAlphaColor;
  APageIndex: Integer;
  AResult: TdxPDFDocumentTextSearchResult;
  AStop: Boolean;
begin
  if not dxPDFViewer1.IsDocumentLoaded then Exit; // Do nothing if the PDF Viewer has no loaded document
  APageIndex := 2;
  ABackColor := dxMakeAlphaColor(100, 200, 100, 100);
  AFrameColor := dxMakeAlphaColor(255, 200, 100, 100);
  dxPDFViewer1.BeginUpdate;
  repeat  // Cycles through all potential text matches
    AResult := dxPDFViewer1.Document.FindText('Dx', TdxPDFDocumentTextSearchOptions.Default, APageIndex);
    case AResult.Status of  // Chooses an appropriate action depending on the search result status
      tssFound:  // If a text match is found...
        begin
          AStop := AResult.Range.PageIndex <> APageIndex;
          if not AStop then
            dxPDFViewer1.Highlights.Add(AResult.Range, ABackColor, AFrameColor);
        end;
    tssNotFound,tssFinished: // Stop searching once the entire document has been processed
      AStop := True;
    end;
  until AStop;
  dxPDFViewer1.Highlights.Visible := bTrue; // Displays all highlights
  dxPDFViewer1.EndUpdate;
end;

Highlighted Search Results

Refer to the following topic for additional information on the text search functionality: How to Search and Highlight Text in PDF Documents.

See Also