> 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/android/search/text-search.md).

# Text search (Android)

Learn how to add text search functionality to your Android viewer with this comprehensive guide. Add search controls, implement SearchToolbar, and utilize the TextSearch API for seamless text searchin

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 Android viewer

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

Text search functionality can be added to your viewer using the text search UI component. Alternatively, you can programmatically perform text search in PDFViewCtrl.

## Prerequisites

* Set up [PDFViewCtrl](/xamarin/guides/android/basics/open.md#view) and [ToolManager](/xamarin/annotation/config-annot-tools.md#android) in your activity.

## Add text search using UI component

Apryse provides [`SearchToolbar`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchToolbar.html) and [`FindTextOverlay`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.FindTextOverlay.html) as utility classes for implementing text search in your document viewer. Here is a short guide on adding text search to your activity:

1. Add the search controls to your layout, a simple example will be something like:

{% code lineNumbers="true" %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">
  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <pdftron.PDF.Controls.SearchToolbar android:id="@+id/searchtoolbar" android:visibility="gone" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" app:contentInsetStart="@dimen/second_keyline" android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />
    <pdftron.PDF.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical|horizontal" />
  </LinearLayout>
  <pdftron.PDF.Controls.FindTextOverlay android:id="@+id/find_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/>
</RelativeLayout>
```

{% endcode %}

1. In `onCreate` of your activity, set up your `SearchToolbar` and `FindTextOverlay`:

{% code lineNumbers="true" %}

```csharp
var searchToolbar = FindViewById<pdftron.PDF.Controls.SearchToolbar>(Resource.Id.searchtoolbar);
var searchOverlay = FindViewById<pdftron.PDF.Controls.FindTextOverlay>(Resource.Id.find_text_view);
searchOverlay.SetPdfViewCtrl(mPdfViewCtrl);

searchToolbar.ExitSearch += (sender, e) =>
{
    searchToolbar.Visibility = ViewStates.Gone;
    searchOverlay.Visibility = ViewStates.Gone;
    searchOverlay.ExitSearchMode();
};
searchToolbar.ClearSearchQuery += (sender, e) =>
{
    searchOverlay?.CancelFindText();
};
searchToolbar.SearchQuerySubmit += (sender, e) =>
{
    searchOverlay?.QueryTextSubmit(e.Query);
};
searchToolbar.SearchQueryChange += (sender, e) =>
{
    searchOverlay?.SetSearchQuery(e.Query);
};
searchToolbar.SearchOptionsItemSelected += (sender, e) =>
{
    int id = e.Item.ItemId;
    if (id == Resource.Id.action_match_case)
    {
        bool isChecked = e.Item.IsChecked;
        searchOverlay?.SetSearchMatchCase(!isChecked);
        searchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
    else if (id == Resource.Id.action_whole_word)
    {
        bool isChecked = e.Item.IsChecked;
        searchOverlay?.SetSearchWholeWord(!isChecked);
        searchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
};
```

{% endcode %}

1. Show the layout

{% code lineNumbers="true" %}

```csharp
searchToolbar.Visibility = ViewStates.Visible;
searchOverlay.Visibility = ViewStates.Visible;
```

{% endcode %}

Now when running the app, you will see a toolbar that allows you to enter search terms as well as an overlay on top of the PDFViewCtrl that navigates among search results.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-458a76a40e34fcc6632629846962476e9ba5c017%2Fe03209a7fd9601c73d1ea180eea01fc2f0df581b-600x1200.gif?alt=media)

## Search results view

[`SearchResultsView`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchResultsView.html) class enables users to easily search for a query and see the results.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-dbe944bce12238ddbb39e44e9942d70cfc8c9ec9%2F26e5ab9a7adb9084eddbb524fa44c1ec790c62eb-1080x2160.png?alt=media)

To set up your layout for a `SearchResultsView`, add an element to your XML layout as follows:

{% code lineNumbers="true" %}

```xml
<com.pdftron.pdf.controls.SearchResultsView android:id="@+id/searchResultsView" android:layout_width="match_parent" android:layout_height="wrap_content" />
```

{% endcode %}

Then, you need to set the following [`events`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchResultsView.html#events)

* An item from the search results is clicked
* A full-text search is started and result has not yet been ready
* A search is found

{% code lineNumbers="true" %}

```csharp
searchResultsView.SetPdfViewCtrl(mPdfViewCtrl);
searchResultsView.FullTextSearchStart += (sender, e) =>
{
    searchToolbar?.SetSearchProgressBarVisible(true);
};
searchResultsView.SearchResultFound += (sender, e) =>
{
    searchToolbar?.SetSearchProgressBarVisible(false);
    searchOverlay?.HighlightFullTextSearchResult(e.Result);
};
searchResultsView.SearchResultClicked += (sender, e) =>
{
    searchOverlay?.HighlightFullTextSearchResult(e.Result);
    mPdfViewCtrl?.SetCurrentPage(e.Result.PageNum);
    searchResultsView.Visibility = ViewStates.Gone;
};
```

{% endcode %}
{% endtab %}

{% tab title="API guide" %}

## Text search API for Android

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/android/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.
