Some test text!

Search
Hamburger Icon

Android / Guides / Text search

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 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">
    
            <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" />
    
            <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" />
        </LinearLayout>
    
        <com.pdftron.pdf.controls.FindTextOverlay
            android:id="@+id/find_text_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />
    </RelativeLayout>
  2. In onCreate of your activity, set up your SearchToolbar and FindTextOverlay:

    private PDFViewCtrl mPdfViewCtrl;
    // ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        // Set up the search controls in our activity
        final SearchToolbar searchToolbar = findViewById(R.id.searchtoolbar);
        final FindTextOverlay searchOverlay = findViewById(R.id.find_text_view);
        searchOverlay.setPdfViewCtrl(mPdfViewCtrl);
        searchToolbar.setSearchToolbarListener(new SearchToolbar.SearchToolbarListener() {
            @Override
            public void onExitSearch() {
                searchToolbar.setVisibility(View.GONE);
                searchOverlay.setVisibility(View.GONE);
                searchOverlay.exitSearchMode();
            }
    
            @Override
            public void onClearSearchQuery() {
                searchOverlay.cancelFindText();
            }
    
            @Override
            public void onSearchQuerySubmit(String s) {
                searchOverlay.queryTextSubmit(s);
            }
    
            @Override
            public void onSearchQueryChange(String s) {
                searchOverlay.setSearchQuery(s);
            }
    
            @Override
            public void onSearchOptionsItemSelected(MenuItem menuItem, String s) {
                int id = menuItem.getItemId();
                if (id == R.id.action_match_case) {
                    boolean isChecked = menuItem.isChecked();
                    searchOverlay.setSearchMatchCase(!isChecked);
                    searchOverlay.resetFullTextResults();
                    menuItem.setChecked(!isChecked);
                } else if (id == R.id.action_whole_word) {
                    boolean isChecked = menuItem.isChecked();
                    searchOverlay.setSearchWholeWord(!isChecked);
                    searchOverlay.resetFullTextResults();
                    menuItem.setChecked(!isChecked);
                }
            }
        });
    }
  3. Show the layout

    searchToolbar.setVisibility(View.VISIBLE);
    searchOverlay.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.

Text Search

Search results view

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

Search View

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

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

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales