Text search

In this document
chevron_rightVia UI controls
chevron_rightVia API
chevron_rightStopping a search
chevron_rightSearch results view

Text search can be done either via high or low level APIs. This tutorial will go through both.

Via UI controls

PDFTron provides a SearchToolbar and a FindTextOverlay as utilities for easy text search. To use these controls, here are a few steps:

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

    <?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: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" />
    <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. Setup PDFViewCtrl and ToolManager

  3. Setup events handling

    SearchToolbar searchToolbar = (SearchToolbar) findViewById(R.id.searchtoolbar);
    FindTextOverlay searchOverlay = (FindTextOverlay) 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);
        }
    }
    });
  4. 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.

Via API

PDFTron provides a number of APIs for finding text in PDF documents. One such example is PDFViewCtrl.findText(String, boolean, boolean, boolean, boolean, int). In order to facilitate the use of text search APIs, a tool class is provided that highlights text search results within the PDF document being viewed. Additionally, a view class is available to show a SearchView, where users can input their queries, change search settings and see the list of search results.

Highlighting results

You can find queries in a document and highlight the search results by using the TextHighlighter tool, call start(String) and specify the search query.

String searchQuery = ...
TextHighlighter highlighter = (TextHighlighter) mToolManager.createTool(ToolMode.TEXT_HIGHLIGHTER, null);
mToolManager.setTool(highlighter);
highlighter.start(searchQuery);

By default, search is done without matching case or matching whole words. In order to change this, use start(String, boolean, boolean, boolean). For example, the followings code searches for a specific query in a specific case and as its own word:

highlighter.start(searchQuery, true, true, false);

The TextHighlighter tool asynchronously searches for instances of the string or regex pattern. To stop a search, call stop(). To clear the highlighted text, call clear().

Changing the highlight colors

To change the color of highlights, use setHighlightColors(int, int, int, int). The following example shows what each argument represents:

int highlightColorInDayMode = ...
int highlightColorInNightMode = ...
int selColorInDayMode = ...
int selColorInNightMode = ...
highlighter.setHighlightColors(highlightColorInDayMode, highlightColorInNightMode, 
    selColorInDayMode, selColorInNightMode);

Search results view

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

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.setListener(searchResultsListener);