Skip to main content

PdfViewer.FindText(String, PdfTextSearchParameters, Func<Int32, Boolean>) Method

Searches for the specified text in the current document with the given parameters and allows you to specify a delegate to terminate the text search.

Namespace: DevExpress.XtraPdfViewer

Assembly: DevExpress.XtraPdfViewer.v23.2.dll

NuGet Package: DevExpress.Win.PdfViewer

Declaration

public PdfTextSearchResults FindText(
    string text,
    PdfTextSearchParameters parameters,
    Func<int, bool> terminate
)

Parameters

Name Type Description
text String

Specifies the text to find in the PDF document.

parameters PdfTextSearchParameters

Contains information related to search results.

terminate Func<Int32, Boolean>

A delegate that encapsulates a method used to terminate the text search on a specific page. The delegate should take the index of the currently processed page and return true to terminate the search; otherwise, false.

Returns

Type Description
PdfTextSearchResults

A PdfTextSearchResults object.

Remarks

Use the FindText method overloads to perform a search in a PDF document. The search starts from the current page defined by the CurrentPageNumber property. When you scroll the document and continue to search the same text again, the search continues from the currently visible page. Use the ContinueSearchFrom property to control how to continue a search in the document: from the current page (the default option) or the last search result.

The FindText method stops the search when it finds the first occurrence of the search term, highlights the occurrence and navigates to the highlighted text. See Search for a Specific Text for details.

When you change the search parameters (the search text, PdfTextSearchParameters.WholeWords, or PdfTextSearchParameters.CaseSensitive), the PDF Viewer starts a new search.

The code snippet below shows how to terminate the text search starting from the page 3 (the page index greater than or equal to 2).

pdfViewer1.FindText(text, parameters, value => value >= 2); 

Note

The FindText method uses the page coordinate system. See the Coordinate Systems topic for more information.

Example

This example shows how to execute the Find Next action by pressing the F3 shortcut, after an end-user has specified text search options in the Find Text dialog.

To do this, handle the PdfViewer.KeyDown event. If the F3 key is pressed, call the overloaded PdfViewer.FindText method and pass the text to search and search parameters obtained from the Find Text dialog to this method.

The text search settings (e.g, search text, whole words, case sensitive) applied by an end-user in the Find Text dialog can be accessed using the PdfViewer.FindDialogOptions property.

using DevExpress.Pdf;
using DevExpress.XtraPdfViewer;
using System;
using System.Windows.Forms;

namespace FindTextUsingShortcut {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            pdfViewer1.LoadDocument(@"..\..\Report.pdf");
        }

        private void pdfViewer1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyData == Keys.F3) {
                PdfTextSearchParameters parameters = new PdfTextSearchParameters();
                PdfFindDialogOptions options = pdfViewer1.FindDialogOptions;
                parameters.CaseSensitive = options.CaseSensitive;
                parameters.WholeWords = options.WholeWords;
                pdfViewer1.FindText(options.Text, parameters);
            }
        }
    }
}
See Also