# 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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixeddocument) when you create a new instance. This is the document that the search targets.

**Create a TextSearch Instance**

```C#
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.                                                                                                                    |

**Searching in a Document**

```C#
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.               |

### Regular Expression and Multi-Word Search

To search for patterns or multi-word phrases, set `UseRegularExpression = true` on `TextSearchOptions`. When using regular expressions, keep `WholeWordsOnly = false` so that internal delimiter wrappers do not conflict with your pattern.

During text extraction:

* Words on the same line are separated by a single space (`" "`).
* Line breaks within the page are normalized to a single space (`" "`) using `TextSearchOptions.TextSearchLinesSeparator`.
* Text search is evaluated per page. Phrases that span page boundaries are not matched.
* In PDF documents, spaces are determined by glyph positioning (`Delta = 1.15`). If text fragments lack sufficient visual displacement, words may be extracted contiguously.

**Search with Regular Expressions and Multi-Word Phrases**

```C#
TextSearch search = new TextSearch(document);
TextSearchOptions options = new TextSearchOptions
{
    UseRegularExpression = true,
    CaseSensitive = false,
    WholeWordsOnly = false
};

// Matches phrases starting with "lorem" followed by a word starting with "a" across spaces or line wraps within the page
IEnumerable<SearchResult> results = search.FindAll(@"lorem\s+a\w*", options);
foreach (SearchResult result in results)
{   
    Console.WriteLine($"Found on page {document.Pages.IndexOf(result.GetResultPage()) + 1}: {result.Result}");
}
```

## See Also

* [RadFixedDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixeddocument)
