Search
This feature allows you to search for a specific text in a PDF document. You can use plain text or a regex for the search criteria. There are various methods that allow you to find all occurrences at once or one by one.
The TextSearch class
This class exposes methods for searching. You need to pass an instance of RadFixedDocument when creating a new instance. This is the document that will be searched.
Example 1: Create 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 TextSeach class exposes the following methods which allow you to search 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 above 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 which allow you to set the search parameters:
| Property | Description |
|---|---|
UseRegularExpression | Gets or sets a value indicating whether a regular expression should be used for searching. |
CaseSensitive | Gets or sets a value indicating whether the search should be case sensitive. |
WholeWordsOnly | Gets or sets a value indicating whether only whole words should be matched. |