Find Text in RadPdfViewer
Use RadPdfViewer to search the text content of a loaded PDF document. This article explains when to use Find, FindAll, and FindPrevious, how to configure TextSearchOptions, and how to work with the returned SearchResult values.
If you want to let users search through the built-in viewer UI, see Find Dialog.
Find Programmatically
To search programmatically:
- Load a document in
RadPdfViewer. - Choose the search method that matches your scenario.
- Optionally create a
TextSearchOptionsinstance. - Check the returned
SearchResultvalue or enumerate all matches.
The programmatic API exposes three main methods:
Find(string text)andFind(string text, TextSearchOptions options)find the next match.FindPrevious(string text)andFindPrevious(string text, TextSearchOptions options)find the previous match.FindAll(string text)andFindAll(string text, TextSearchOptions options)return all matches in the current document.
Choose the Right Search Method
Use the search method that matches the navigation behavior you need:
- Use
Findto move forward to the next match. - Use
FindPreviousto move backward through existing matches. - Use
FindAllwhen you need the full set of results, for example to build your own results list or count all occurrences.
Find the Next Match
Use Find when you want the next occurrence of a string, optionally with custom search options.
Example 1: Find the next match with custom options
string sampleText = "sample text";
TextSearchOptions textSearchOptions =
new TextSearchOptions(caseSensitive: false, useRegularExpression: false, wholeWordsOnly: true);
SearchResult searchResult = this.pdfViewer.Find(sampleText, textSearchOptions);Find All Matches
Use FindAll when you need every match in the current document instead of only the next or previous one.
Example 2: Find all matches with custom options
string sampleText = "sample text";
TextSearchOptions textSearchOptions =
new TextSearchOptions(caseSensitive: false, useRegularExpression: false, wholeWordsOnly: true);
IEnumerable<SearchResult> searchResults = this.pdfViewer.FindAll(sampleText, textSearchOptions);Find the Previous Match
Use FindPrevious when the current user flow needs to go back to an earlier occurrence.
Example 3: Find the previous match with custom options
string sampleText = "sample text";
TextSearchOptions textSearchOptions =
new TextSearchOptions(caseSensitive: false, useRegularExpression: false, wholeWordsOnly: true);
SearchResult searchResult = this.pdfViewer.FindPrevious(sampleText, textSearchOptions);SearchResult
The SearchResult class describes a single search hit. After calling Find or FindPrevious, compare the returned value with SearchResult.NotFound before accessing its properties.
Use these members most often:
NotFoundreturns the default result when no match exists.Rangereturns a TextRange that describes where the match starts and ends.Resultreturns the matched text.ToString()returns a string representation of the current result.
Example 4: Check whether a match was found
SearchResult searchResult = this.pdfViewer.Find("sample text");
if (searchResult != SearchResult.NotFound)
{
TextRange searchResultRange = searchResult.Range;
string searchResultAsText = searchResult.Result;
}Configure TextSearchOptions
Use TextSearchOptions to control how the search engine interprets the input text.
The class provides the following constructors:
TextSearchOptions(bool caseSensitive)TextSearchOptions(bool caseSensitive, bool useRegularExpression)TextSearchOptions(bool caseSensitive, bool useRegularExpression, bool wholeWordsOnly)
Use the options as follows:
- Set
CaseSensitivetotruewhen letter casing must match exactly. - Set
UseRegularExpressiontotruewhen the search text is a regular expression pattern. - Set
WholeWordsOnlytotruewhen partial-word matches must be excluded.
The class also exposes the static Default property, which returns the default search configuration.
Example 5: Create the default search options explicitly
TextSearchOptions textSearchOptions =
new TextSearchOptions(caseSensitive: false, useRegularExpression: false, wholeWordsOnly: false);Example 6: Use the default search options
TextSearchOptions textSearchOptions = TextSearchOptions.Default;The main instance properties are:
UseRegularExpression: Gets or sets whether the search uses a regular expression.CaseSensitive: Gets or sets whether the search respects letter casing.WholeWordsOnly: Gets or sets whether only whole-word matches are returned.
The class also exposes the PropertyChanged event, which occurs when a property value changes.
Common Search Workflow
The following example shows a typical search flow:
- Define the search text.
- Create search options.
- Call
Find. - Check whether a result was found.
Example 7: Run a complete search flow
string sampleText = "invoice";
TextSearchOptions options = new TextSearchOptions(false, false, true);
SearchResult result = this.pdfViewer.Find(sampleText, options);
if (result == SearchResult.NotFound)
{
return;
}
string matchedText = result.Result;
TextRange matchedRange = result.Range;