PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Text handling in the viewer usually falls into two different tasks:
This page covers both capabilities, but they serve different purposes:
If the goal is to retrieve text that was manually selected inside the viewer, see Get the Selected Content.
Keyword search is commonly used in the following scenarios:
The search flow usually includes these steps:
ITextSearcherList<CPDFTextRange> searchTextInfoList = new ArrayList<>();
ITextSearcher textSearcher = readerView.getTextSearcher();
String keywords = "ComPDF";
textSearcher.setSearchConfig(
keywords,
CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseSensitive
);
for (int i = 0; i < document.getPageCount(); i++) {
CPDFPage page = document.pageAtIndex(i);
CPDFTextPage textPage = page.getTextPage();
if (textPage == null || !textPage.isValid()) {
continue;
}
List<CPDFTextRange> searchPageContent = textSearcher.searchKeyword(i);
if (!searchPageContent.isEmpty()) {
searchTextInfoList.addAll(searchPageContent);
}
}val searchTextInfoList = mutableListOf<CPDFTextRange>()
val textSearcher: ITextSearcher = readerView.getTextSearcher()
val keywords = "ComPDF"
textSearcher.setSearchConfig(
keywords,
CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseSensitive
)
for (i in 0 until document.pageCount) {
val page = document.pageAtIndex(i)
val textPage = page.textPage
if (textPage == null || !textPage.isValid) {
continue
}
val searchPageContent = textSearcher.searchKeyword(i)
if (searchPageContent.isNotEmpty()) {
searchTextInfoList.addAll(searchPageContent)
}
}setSearchConfig(...) supports these common options:
| Option | Description | Value |
|---|---|---|
PDFSearchCaseInsensitive | Case-insensitive match | 0 |
PDFSearchCaseSensitive | Case-sensitive match | 1 |
PDFSearchMatchWholeWord | Match the whole word | 2 |
Search options should be chosen based on the business goal. Full-text search usually fits case-insensitive matching, while exact term matching is better suited to case-sensitive or whole-word matching.
If the screen must show search summaries, preview snippets, or surrounding text, the matched content can be extracted from CPDFTextRange.
The following example shows how to read the matched text and a short context window.
int pageIndex = 0;
List<CPDFTextRange> searchPageContent = textSearcher.searchKeyword(pageIndex);
if (searchPageContent.isEmpty()) {
return;
}
CPDFTextRange textRange = searchPageContent.get(0);
CPDFPage page = document.pageAtIndex(pageIndex);
CPDFTextPage textPage = page.getTextPage();
String text = textPage.getText(textRange);
int targetStart = textRange.location - 20;
int length;
if (targetStart > 0) {
length = textRange.length + 40;
} else {
length = textRange.length + 40 + targetStart;
targetStart = 0;
}
CPDFTextRange targetTextRange = new CPDFTextRange(targetStart, length);
String contextText = textPage.getText(targetTextRange);When handling single-page results, make sure that the CPDFTextRange belongs to the current page's CPDFTextPage to avoid cross-page reads.
When matched results need to be highlighted inside the viewer, the following methods are available.
int pageIndex = 0;
int textRangeIndex = 0;
textSearcher.searchBegin(pageIndex, textRangeIndex);
readerView.invalidateAllChildren();textSearcher.searchBackward();textSearcher.searchForward();textSearcher.cancelSearch();In addition to keyword search, CPDFPage and CPDFTextPage also support extracting text from a rectangular region. This is useful for:
The following example defines a rectangle in page coordinates, then converts it into the coordinate range used for text extraction on the current page.
CPDFPage pdfPage = document.pageAtIndex(0);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
selectRect = pdfPage.convertRectFromPage(
false,
pdfPage.getSize().width(),
pdfPage.getSize().height(),
selectRect
);
CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);
for (CPDFTextSelection textSelection : textSelectionArr) {
if (textSelection == null) {
continue;
}
String text = pdfTextPage.getText(textSelection.getTextRange());
}val pdfPage = document.pageAtIndex(0)
val pdfTextPage = pdfPage.textPage
var selectRect = pdfPage.convertRectFromPage(
false,
pdfPage.size.width(),
pdfPage.size.height(),
RectF(0f, 0f, 500f, 500f)
)
val textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect)
for (textSelection in textSelectionArr) {
val text = pdfTextPage.getText(textSelection.textRange)
}This extracts text from a defined rectangular range. It is not the same as retrieving text that was manually selected through a long press in the viewer.
pageIndex and CPDFTextRange.