Search
The search feature allows you to find specific text in a PDF document. You can use plain text or a regular expression for the search criteria. Various methods allow you to find all occurrences at once or one by one.
The TextSearch Class
The TextSearch class exposes methods for searching. Pass an instance of RadFixedDocument when you create a new instance. This is the document that the search targets.
Example 1: Create a TextSearch Instance
PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument document = provider.Import(File.ReadAllBytes(@"Test.pdf"), TimeSpan.FromSeconds(10));
TextSearch search = new TextSearch(document);
IEnumerable<SearchResult> result = search.FindAll("Lorem", TextSearchOptions.Default);
Search Methods
The TextSearch class exposes the following methods for searching the document:
| Method | Description |
|---|---|
Find(string text, TextSearchOptions options) | Finds the first occurrence of the specified string starting from the beginning of the document. Optionally accepts a start position or a specific range. |
FindPrevious(string text, TextSearchOptions options) | Finds the previous occurrence of the specified text. Optionally accepts a start position or a specific range. |
FindAll(string text, TextSearchOptions options) | Finds all occurrences of the specified text. Optionally accepts a start position or a specific range. |
SearchResult Class
All of the listed methods return one or more instances of the SearchResult class. This class exposes the following public members that provide information about the results:
| Member | Description |
|---|---|
TextRange | An object describing the start and end positions of the match. Exposes StartPosition (start index and location) and EndPosition (end index and location). |
Result | A string value representing the match. |
GetWordBoundingRect() | Gets the rectangle of the current match. |
GetResultPage() | Gets the page where the current result is. |
Example 2: Searching in a Document
PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument document = provider.Import(File.ReadAllBytes(@"Test.pdf"), TimeSpan.FromSeconds(10));
TextSearch search = new TextSearch(document);
IEnumerable<SearchResult> result = search.FindAll("Lorem", TextSearchOptions.Default);
foreach (SearchResult resultItem in result)
{
Rect rect = resultItem.GetWordBoundingRect();
RadFixedPage page = resultItem.GetResultPage();
FixedContentEditor editor = new FixedContentEditor(page);
editor.DrawRectangle(rect);
}
File.WriteAllBytes(@"result.pdf", provider.Export(document, TimeSpan.FromSeconds(10)));
TextSearchOptions
The TextSearchOptions class exposes the following properties for setting the search parameters:
| Property | Description |
|---|---|
UseRegularExpression | Gets or sets a value indicating whether a regular expression is used for searching. |
CaseSensitive | Gets or sets a value indicating whether the search is case sensitive. |
WholeWordsOnly | Gets or sets a value indicating whether only whole words are matched. |