Text Search
Foxit PDF SDK for Android provides in-document text search through the Core SDK, supporting PDF documents (including XFA), individual text pages, and annotation appearances. Results include page index, character range, rectangles, and matching sentence.
Built-in search in UI Extensions
If you use the full reader (UI Extensions), text search is built in. Users open the search panel from the toolbar with keyword input, case sensitivity, whole word, result highlighting, and prev/next navigation. No extra code is required.
Core Classes
| Class | Description |
|---|---|
TextSearch | Text search engine; construct from PDFDoc, XFADoc, TextPage, or Annot |
SearchCancelCallback | Search cancel callback; override needToCancelNow() to abort during search |
Search Flag Constants
Set matching rules with setSearchFlags(int flags); combine with bitwise OR:
| Constant | Value | Description |
|---|---|---|
e_SearchNormal | 0x00 | Default; case insensitive |
e_SearchMatchCase | 0x01 | Match case |
e_SearchMatchWholeWord | 0x02 | Whole word |
e_SearchConsecutive | 0x04 | Continue from character after last match |
e_SearchNotMatchFullWidth | 0x20 | Do not match full-width characters |
Example: Search in a Document
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.TextPage;
import com.foxit.sdk.pdf.TextSearch;
import com.foxit.sdk.common.fxcrt.RectFArray;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
TextSearch textSearch = new TextSearch(doc, null, TextPage.e_ParseTextNormal);
textSearch.setStartPage(0);
textSearch.setPattern("foxit");
textSearch.setSearchFlags(TextSearch.e_SearchMatchCase | TextSearch.e_SearchMatchWholeWord);
while (textSearch.findNext()) {
int pageIndex = textSearch.getMatchPageIndex();
int startChar = textSearch.getMatchStartCharIndex();
int endChar = textSearch.getMatchEndCharIndex();
RectFArray rects = textSearch.getMatchRects();
String sentence = textSearch.getMatchSentence();
}Example: Search in Annotation Appearance
java
import com.foxit.sdk.pdf.TextSearch;
import com.foxit.sdk.pdf.annots.Annot;
Annot annot = page.getAnnot(0);
TextSearch textSearch = new TextSearch(annot);
textSearch.setPattern("keyword");
textSearch.setSearchFlags(TextSearch.e_SearchNormal);
while (textSearch.findNext()) {
RectFArray rects = textSearch.getMatchRects();
}Example: Cancel Callback
java
SearchCancelCallback cancelCallback = new SearchCancelCallback() {
@Override
public boolean needToCancelNow() {
return shouldCancel;
}
};
TextSearch textSearch = new TextSearch(doc, cancelCallback, TextPage.e_ParseTextNormal);Note
To index and search multiple PDFs in an offline directory, see Indexed Full-Text Search.
API Reference
See the API Reference for full TextSearch and SearchCancelCallback documentation.