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 TextSerch Instance
PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument document = provider.Import(File.ReadAllBytes(@"Test.pdf"));
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:
- Find(string text, TextSearchOptions options): Finds the first occurrence of the specified string starting from the beginning of the document. Optionally you can specify a start position or a specific range.
- FindPrevious(string text, TextSearchOptions options): Finds the previous occurrence of the specified text. Optionally you can specify a start position or a specific range.
- FindAll(string text, TextSearchOptions options): Finds all occurrences of the specified text. Optionally you can specify 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.
-
TextRange: An object describing the start and end positions of the match. This object exposed the following properties.
- StartPosition: An object that contains the start index and location of the current match.
- EndPosition An object that contains the end index and location of the current match.
- 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"));
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));
TextSearchOptions
The TextSearchOptions class exposes the following properties which allow you to set the search parameters.
- 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.