> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/xamarin/guides/ios/search/text-search.md).

# Text search (iOS)

Learn how to add text search functionality to your Xamarin.iOS app with this comprehensive API guide and UI component tutorial. Enhance user experience and optimize document navigation. The Apryse Xam

There are two parts to text searching. First is the UI component that is presented to the user. Second is an API guide to perform the text search functionality.

{% tabs %}
{% tab title="UI component" %}

## Add text search to Xamarin.iOS viewer

{% hint style="info" %}
**This tutorial only applies to Xamarin.iOS. See Xamarin.Android equivalent here .**
{% endhint %}

The [`PTTextSearchViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTextSearchViewController.html) class allows the user to enter and search text in a document. Search results are automatically highlighted in the document and a toolbar with navigation buttons allows searching forward or backward through the document. The core text search functionality is provided by the `PTPDFViewCtrl`'s text search methods.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-5b996e58859c24b85e6b62bc67642b88c7b6715f%2F751784ed5090a448a7c6c10669313b4ca6f4f856-1242x2208.png?alt=media)

The search view controller is part of the Tools library, so make sure you have [added the Tools library to your project](/xamarin/ui-customization/setup.md).

## Showing a text search view controller

To create and set up a text search view controller, supply a `PTPDFViewCtrl` instance to the `PTTextSearchViewController` designated initializer:

{% code lineNumbers="true" %}

```csharp
var textSearchViewController = new PTTextSearchViewController(mPdfViewCtrl);
textSearchViewController.SearchViewControllerDidDismiss += (sender, e) =>
{
    
};
var navigationController = new UINavigationController(textSearchViewController);

if (UserInterfaceIdiomIsPhone)
{
    navigationController.ModalPresentationStyle = UIModalPresentationStyle.Custom;
}
else
{
    navigationController.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    navigationController.PopoverPresentationController.BarButtonItem = searchButton;
}

this.PresentViewController(navigationController, true, null);
```

{% endcode %}

{% hint style="info" %}
**The search view controller does not create its own top navigation bar, so it must be pushed onto a navigation controller's stack.**
{% endhint %}

## Search results list view

The [`PTTextSearchViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTextSearchViewController.html) also presents the search results in a [`UITableView`](https://developer.apple.com/uikit/uitableview/) for fast and easy navigation. Selecting any result in the table view will immediately navigate to that result and highlight it.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-0f6e4cec66513b1e061b53385da57beb32df4d8b%2F08cf4fd3cf391932800026c39ccb26afb382d8d2-1242x2208.png?alt=media)

## Search options

The [`PTTextSearchViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTextSearchViewController.html) interface also allows the user to refine the search to be case-sensitive, or to match whole words only:

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-78bd27828d88921be427867d6e894bdf06e92ec3%2Fa495a97d93efa8972f0dce4876598b822261e148-1242x2208.png?alt=media)

## The text search view controller delegate

The [`PTTextSearchViewControllerDelegate`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTextSearchViewControllerDelegate.html) protocol allows the adopting class (usually the containing view controller, as in this guide) to be notified when the view controller has been dismissed. This can be used to show or hide other content as appropriate.
{% endtab %}

{% tab title="API guide" %}

## Text search API for Xamarin.iOS

To search for text in a PDF using regular expression and then apply a link annotation on the highlighted result.

{% hint style="info" %}
In this example, we add a link annotation but any other types of annotations can be applied here such as redaction annotations in the case of a search and redact workflow.
{% endhint %}

{% code lineNumbers="true" %}

```csharp
PDFDoc doc = new PDFDoc(filename);
Int32 page_num = 0;
String result_str = "", ambient_string = "";
Highlights hlts = new Highlights();
TextSearch txt_search = new TextSearch();
Int32 mode = (Int32)(TextSearch.SearchMode.e_whole_word | TextSearch.SearchMode.e_page_stop | TextSearch.SearchMode.e_highlight);
String pattern = "";

//use regular expression to find credit card number
mode |= (Int32)(TextSearch.SearchMode.e_reg_expression | TextSearch.SearchMode.e_highlight);
txt_search.SetMode(mode);
String pattern = "\\d{4}-\\d{4}-\\d{4}-\\d{4}"; //or "(\\d{4}-){3}\\d{4}"
txt_search.SetPattern(pattern);

//call Begin() method to initialize the text search.
txt_search.Begin( doc, pattern, mode, -1, -1 );
TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, hlts );

if ( code == TextSearch.ResultCode.e_found )
{
  //add a link annotation based on the location of the found instance
  hlts.Begin(doc);
  while (hlts.HasNext())
  {
    Page cur_page = doc.GetPage(hlts.GetCurrentPageNumber());
    double[] quads = hlts.GetCurrentQuads();
    int quad_count = quads.Length / 8;
    for (int i = 0; i < quad_count; ++i)
    {
      //assume each quad is an axis-aligned rectangle
      int offset = 8 * i;
      double x1 = Math.Min(Math.Min(Math.Min(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
      double x2 = Math.Max(Math.Max(Math.Max(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
      double y1 = Math.Min(Math.Min(Math.Min(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);
      double y2 = Math.Max(Math.Max(Math.Max(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);

      Annots.Link hyper_link = Annots.Link.Create(doc, new Rect(x1, y1, x2, y2), Action.CreateURI(doc, "http://www.apryse.com"));
      hyper_link.RefreshAppearance();
      cur_page.AnnotPushBack(hyper_link);
    }
    hlts.Next();
  }
}
```

{% endcode %}

[Search PDF files for text](/xamarin/get-started/samples.md#textsearch) Full code sample which shows how to use TextSearch to search text on PDF pages using regular expressions.
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/xamarin/guides/ios/search/text-search.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
