Text search (Android)

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.

Add text search to Android viewer

This tutorial only applies to Xamarin.Android. See Xamarin.iOS equivalent here .

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

Add text search using UI component

Apryse provides SearchToolbar and FindTextOverlay 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:

XML

1<?xml version="1.0" encoding="utf-8"?>
2<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">
3 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
4 <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" />
5 <pdftron.PDF.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical|horizontal" />
6 </LinearLayout>
7 <pdftron.PDF.Controls.FindTextOverlay android:id="@+id/find_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/>
8</RelativeLayout>
  1. In onCreate of your activity, set up your SearchToolbar and FindTextOverlay:

C#

1var searchToolbar = FindViewById<pdftron.PDF.Controls.SearchToolbar>(Resource.Id.searchtoolbar);
2var searchOverlay = FindViewById<pdftron.PDF.Controls.FindTextOverlay>(Resource.Id.find_text_view);
3searchOverlay.SetPdfViewCtrl(mPdfViewCtrl);
4
5searchToolbar.ExitSearch += (sender, e) =>
6{
7 searchToolbar.Visibility = ViewStates.Gone;
8 searchOverlay.Visibility = ViewStates.Gone;
9 searchOverlay.ExitSearchMode();
10};
11searchToolbar.ClearSearchQuery += (sender, e) =>
12{
13 searchOverlay?.CancelFindText();
14};
15searchToolbar.SearchQuerySubmit += (sender, e) =>
16{
17 searchOverlay?.QueryTextSubmit(e.Query);
18};
19searchToolbar.SearchQueryChange += (sender, e) =>
20{
21 searchOverlay?.SetSearchQuery(e.Query);
22};
23searchToolbar.SearchOptionsItemSelected += (sender, e) =>
24{
25 int id = e.Item.ItemId;
26 if (id == Resource.Id.action_match_case)
27 {
28 bool isChecked = e.Item.IsChecked;
29 searchOverlay?.SetSearchMatchCase(!isChecked);
30 searchOverlay?.ResetFullTextResults();
31 e.Item.SetChecked(!isChecked);
32 }
33 else if (id == Resource.Id.action_whole_word)
34 {
35 bool isChecked = e.Item.IsChecked;
36 searchOverlay?.SetSearchWholeWord(!isChecked);
37 searchOverlay?.ResetFullTextResults();
38 e.Item.SetChecked(!isChecked);
39 }
40};
  1. Show the layout

C#

1searchToolbar.Visibility = ViewStates.Visible;
2searchOverlay.Visibility = ViewStates.Visible;

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.

Apryse Docs Image

Search results view

SearchResultsView class enables users to easily search for a query and see the results.

Apryse Docs Image

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

XML

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

Then, you need to set the following 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

C#

1searchResultsView.SetPdfViewCtrl(mPdfViewCtrl);
2searchResultsView.FullTextSearchStart += (sender, e) =>
3{
4 searchToolbar?.SetSearchProgressBarVisible(true);
5};
6searchResultsView.SearchResultFound += (sender, e) =>
7{
8 searchToolbar?.SetSearchProgressBarVisible(false);
9 searchOverlay?.HighlightFullTextSearchResult(e.Result);
10};
11searchResultsView.SearchResultClicked += (sender, e) =>
12{
13 searchOverlay?.HighlightFullTextSearchResult(e.Result);
14 mPdfViewCtrl?.SetCurrentPage(e.Result.PageNum);
15 searchResultsView.Visibility = ViewStates.Gone;
16};

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales