Text Search ​
Foxit PDF SDK for Web provides two types of text search APIs: PDFDoc.getTextSearch() / PDFPage.getTextSearch() return search objects for sequential lookup; PDFDoc.searchText() searches one or more keywords at once within a specified page range.
With UIExtension, the search panel is usually built into the UI so users can open search from the toolbar and browse results. This topic explains text search through APIs. For sequential full-text/region extraction or character-level geometry, see Text extraction. To find and replace page text, see the Adv Edit find-and-replace API in PDF edit modules.
Search APIs ​
| Task | API |
|---|---|
| Sequential search in a document | PDFDoc.getTextSearch() |
| Sequential search on one page | PDFPage.getTextSearch() |
| Read sequential search results | TextSearchMatch |
| Batch search in a page range | PDFDoc.searchText() |
Sequential Search with a Search Object ​
PDFDoc.getTextSearch(pattern, flags) returns DocTextSearch. It suits search panels, previous/next result navigation, starting search from a given page, and highlighting matches one at a time.
javascript
const pdfDoc = pdfViewer.getCurrentPDFDoc();
const textSearch = pdfDoc.getTextSearch('Foxit', 1 | 2);
const match = await textSearch.findNext();
if (match) {
console.log(match.getPageIndex());
console.log(match.getSentence());
console.log(match.getRects());
}Common DocTextSearch APIs:
| API | Description |
|---|---|
findNext() | Finds the next match. |
findPrev() | Finds the previous match. |
setCurrentPageIndex(index) | Sets the current search page. |
setStartPageIndex(index) | Sets the search start page. |
setEndPageIndex(index) | Sets the search end page. |
destroy() | Destroys the search object. |
findNext() and findPrev() return TextSearchMatch or null. TextSearchMatch exposes page index, character range, rectangles, and the containing sentence.
javascript
const match = await textSearch.findNext();
if (match) {
const pageIndex = match.getPageIndex();
const rects = match.getRects();
const sentence = match.getSentence();
const startCharIndex = match.getStartCharIndex();
const endCharIndex = match.getEndCharIndex();
}To search only one page, get PDFPage first, then use PDFPage.getTextSearch(pattern, flags).
javascript
const page = await pdfDoc.getPageByIndex(0);
const pageTextSearch = await page.getTextSearch('Foxit', 0);
const match = await pageTextSearch.findNext();
if (match) {
console.log(match.getRects());
}
pageTextSearch.destroy();When you no longer need the search object, call destroy() to release related resources.
Search Flags ​
getTextSearch(pattern, flags) uses bit flags to control matching rules; combine them with bitwise OR:
| Flag | Description |
|---|---|
0 | Normal search. |
1 | Case sensitive. |
2 | Whole word match. |
4 | Consecutive match. For example, searching for CC in CCC can match multiple times. |
Batch Text Search ​
PDFDoc.searchText(pages, words, options) searches one or more keywords at once in specified pages. It suits business automation, batch positioning, sensitive-word lookup, or integration with annotation and redaction features.
javascript
const pdfDoc = pdfViewer.getCurrentPDFDoc();
const result = await pdfDoc.searchText(
[0, 1],
['Foxit', 'PDF'],
{
wholeWordsOnly: true,
caseSensitive: false
}
);
console.log(result);Results are grouped by page index. Each match usually contains the matched text and corresponding rectangles.
javascript
{
0: [
{
word: 'Foxit',
rects: [
{ left: 100, right: 145, top: 720, bottom: 700 }
]
}
],
1: []
}PDFDoc.searchText() accepts an options object:
| Option | Description |
|---|---|
wholeWordsOnly | Match whole words only. |
caseSensitive | Case sensitive. |
How to Choose ​
- Use
PDFDoc.getTextSearch()for previous/next navigation, search panels, sequential highlighting, or starting from a specific page. - Use
PDFPage.getTextSearch()when searching a single page only. - Use
PDFDoc.searchText()when you need rectangle results for multiple keywords across multiple pages at once. - Prefer
PDFDoc.searchText()for sensitive-word positioning, batch marking, or redaction workflows.
Integration with Redaction ​
Rectangles from search results can be used to create redaction marks on pages. For sensitive words, call PDFDoc.searchText() first to get match locations, then follow Redaction to create redaction marks and apply redaction with applyRedaction and related APIs.
javascript
const result = await pdfDoc.searchText([0], ['secret'], {
wholeWordsOnly: true,
caseSensitive: false
});
const page = await pdfDoc.getPageByIndex(0);
const rects = result[0].flatMap(item => item.rects);
if (rects.length > 0) {
await page.markRedactAnnot(rects);
}Notes ​
- Page indexes start at
0. - Search results depend on parseable text in the PDF; scanned documents usually require OCR before text can be searched.
- Full-document search on large files can take time. Limit page ranges with
searchText(); withgetTextSearch(), search sequentially as needed. - Rectangles in search results use PDF coordinates, not Viewer device pixels.