Text search

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

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
4 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
5
6 <com.pdftron.pdf.controls.SearchToolbar android:id="@+id/searchtoolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:visibility="gone" app:contentInsetStart="@dimen/second_keyline" app:navigationIcon="@drawable/ic_arrow_back_white_24dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
7
8 <com.pdftron.pdf.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical|horizontal" />
9 </LinearLayout>
10
11 <com.pdftron.pdf.controls.FindTextOverlay android:id="@+id/find_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" />
12</RelativeLayout>
  1. In onCreate of your activity, set up your SearchToolbar and FindTextOverlay:
1private PDFViewCtrl mPdfViewCtrl;
2// ...
3@Override
4protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 // ...
7 // Set up the search controls in our activity
8 final SearchToolbar searchToolbar = findViewById(R.id.searchtoolbar);
9 final FindTextOverlay searchOverlay = findViewById(R.id.find_text_view);
10 searchOverlay.setPdfViewCtrl(mPdfViewCtrl);
11 searchToolbar.setSearchToolbarListener(new SearchToolbar.SearchToolbarListener() {
12 @Override
13 public void onExitSearch() {
14 searchToolbar.setVisibility(View.GONE);
15 searchOverlay.setVisibility(View.GONE);
16 searchOverlay.exitSearchMode();
17 }
18
19 @Override
20 public void onClearSearchQuery() {
21 searchOverlay.cancelFindText();
22 }
23
24 @Override
25 public void onSearchQuerySubmit(String s) {
26 searchOverlay.queryTextSubmit(s);
27 }
28
29 @Override
30 public void onSearchQueryChange(String s) {
31 searchOverlay.setSearchQuery(s);
32 }
33
34 @Override
35 public void onSearchOptionsItemSelected(MenuItem menuItem, String s) {
36 int id = menuItem.getItemId();
37 if (id == R.id.action_match_case) {
38 boolean isChecked = menuItem.isChecked();
39 searchOverlay.setSearchMatchCase(!isChecked);
40 searchOverlay.resetFullTextResults();
41 menuItem.setChecked(!isChecked);
42 } else if (id == R.id.action_whole_word) {
43 boolean isChecked = menuItem.isChecked();
44 searchOverlay.setSearchWholeWord(!isChecked);
45 searchOverlay.resetFullTextResults();
46 menuItem.setChecked(!isChecked);
47 }
48 }
49 });
50}
  1. Show the layout
1searchToolbar.setVisibility(View.VISIBLE);
2searchOverlay.setVisibility(View.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 SearchResultsListener to receive a callback when

  • 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
1SearchResultsView searchResultsView = view.findViewById(R.id.searchResultsView);
2searchResultsView.setPdfViewCtrl(mPdfViewCtrl);
3searchResultsView.setListener(searchResultsListener);

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales