Foxit PDF SDK for Android

How to perform Indexed Full-Text Search in Foxit PDF SDK for Android?

Foxit PDF SDK for Android supported Indexed Full Text Search since version 5.0. To use this feature, follow the steps below:

1) Get document source information. Create a document source based on a directory which will be used as the search directory.

public DocumentsSource(String directory)

2) Create a full text search object, and set a path of database to store the indexed data.

public FullTextSearch()
public void setDataBasePath(String pathOfDataBase)

3) Start to index the PDF documents which receive from the source.

public Progressive startUpdateIndex(DocumentsSource source,
        PauseCallback pause, boolean reUpdate)

Note: You can index a specified PDF file. For example, if the contents of a PDF file have been changed, you can re-index it using the following API:

public boolean updateIndexWithFilePath(java.lang.String filePath)

4) Search the specified keyword from the indexed data source. The search results will be returned to external by a specified callback function when a matched one is found.

public boolean searchOf(java.lang.String matchString,
        RankMode rankMode,
        SearchCallback searchCallback)

Following is a sample for how to use it:

String directory = "A search directory...";
FullTextSearch search = new FullTextSearch();
try {
    String dbPath = "The path of data base to store the indexed data...";
    search.setDataBasePath(dbPath);
    // Get document source information.
    DocumentsSource source = new DocumentsSource(directory);

    // Create a Pause callback object implemented by users to pause the updating process.
    PauseUtil pause = new PauseUtil(30);

    // Start to update the index of PDF files which receive from the source.
    Progressive progressive = search.startUpdateIndex(source, pause, false);
    int state = Progressive.e_ToBeContinued;
    while (state == Progressive.e_ToBeContinued) {
        state = progressive.resume();
    }

    // Create a callback object which will be invoked when a matched one is found.
    MySearchCallback searchCallback = new MySearchCallback();

    // Search the specified keyword from the indexed data source.
    search.searchOf("looking for this text", RankMode.e_RankHitCountASC, searchCallback);
} catch (PDFException e) {
    e.printStackTrace();
}

A sample callback for PauseUtil is as follows:

public class PauseUtil extends PauseCallback{
    private long m_milliseconds = 0;
    private long m_startTime = 0;

    public PauseUtil(long milliSeconds) {
        Date date = new Date();
        m_milliseconds = milliSeconds;
        m_startTime = date.getTime();
    }

    @Override
    public boolean needToPauseNow() {
        // TODO Auto-generated method stub
        if (this.m_milliseconds < 1) return false; Date date = new Date(); long diff = date.getTime() - m_startTime; if (diff > this.m_milliseconds) {
            m_startTime = date.getTime();
            return true;
        } else
            return false;
    }
}

A sample callback for MySearchCallback is as follows:

public class MySearchCallback extends SearchCallback {
    private static final String TAG = MySearchCallback.class.getCanonicalName();

    @Override
    public void release() {
    }

    @Override
    public int retrieveSearchResult(String filePath, int pageIndex, String matchResult, int matchStartTextIndex, int matchEndTextIndex) {
        String s = String.format("Found file is :%s \n Page index is :%d Start text index :%d End text index :%d \n Match is :%s \n\n", filePath, pageIndex, matchStartTextIndex, matchEndTextIndex, matchResult);
        Log.v(TAG, "retrieveSearchResult: " + s);
        return 0;
    }
}

Note:

  • The indexed full text search provided by Foxit PDF SDK for Android will go through a directory recursively, so that both the files and the folders under the search directory will be indexed.
  • If you want to abort the index process, you can pass in a pause callback parameter to the startUpdateIndex interface. The callback function needToPauseNow will be invoked once a PDF document is indexed, so that the caller can abort the index process when the callback needToPauseNow return “true”.
  • The location of the indexed database is set by setDataBasePath interface. If you want to clear the indexed database, you shoud do it manually. And now, removing a file from index function is not supported.
  • Every search result of the searchOf interface is returned to external by a specified callback. Once the searchOf interface returns “true” or “false”, it means the searching is finished.

Updated on August 29, 2018

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: