Text Search & Selection                        


Text Search


ComPDFKit PDF SDK offers developers an API for programmatic full-text search, as well as UI for searching and highlighting relevant matches.


- Get an ITextSearcher to complete all search operations.

ITextSearcher textSearcher = readerView.getTextSearcher()


- Before triggering a search, you can configure various search options:

CPDFSearchOptions::PDFSearchCaseInsensitive: Case insensitive.

CPDFSearchOptions::PDFSearchCaseSensitive: Case sensitive.

CPDFSearchOptions::PDFSearchMatchWholeWord: Match whole word.


textSearcher.setSearchConfig(searchString, T、CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseInsensitive);


- Synchronously finds all instances of the specified string in the document, use CPDFTextRange ArrayList for each page. If there are no hits, this method returns an empty array.


ArrayList textRanges = textSearcher.searchKeyword(pageIndex);


- Highlight search results


Full code sample which shows how to use ITextSearcher to search and replace text strings and highlight in existing PDF. Set the searcher to search from the keyword textRangeIndex on page pageIndex:

textSearcher.searchBegin(pageIndex, textRangeIndex);


Text Selection


PDF text contents are stored in CPDFPage objects which are related to a specific page. CPDFPage class can be used to retrieve information about text in a PDF page, such as single character, single word, text content within specified character range or bounds and more.


How to get the text bounds on a page by selection:


RectF size = readerView.getPageNoZoomSize(1);
CPDFPage pdfPage = readerView.getPDFDocument().pageAtIndex(1);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
selectRect = pdfPage.convertRectFromPage(readerView.isCropMode(), size.width(), size.height(), selectRect);
TextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);