Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Guides

Search Text and Extract Range Content ​

Text handling in the viewer usually falls into two different tasks:

  1. search the entire document for a keyword and locate the matched results
  2. extract text from a specific page range for summaries, previews, or secondary processing

This page covers both capabilities, but they serve different purposes:

  • keyword search is about locating matches and navigating to them
  • range-based extraction is about reading text from a defined region

If the goal is to retrieve text that was manually selected inside the viewer, see Get the Selected Content.

Search for keywords across the document ​

Keyword search is commonly used in the following scenarios:

  • locating specific terms inside long documents
  • building a search results list
  • highlighting results in the current reading view

Create the searcher and iterate through pages ​

The search flow usually includes these steps:

  1. create a result collection
  2. get ITextSearcher
  3. set the keyword and search options
  4. iterate through pages and save matched results
java
List<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);
  }
}
kotlin
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)
  }
}

Choose search options ​

setSearchConfig(...) supports these common options:

OptionDescriptionValue
PDFSearchCaseInsensitiveCase-insensitive match0
PDFSearchCaseSensitiveCase-sensitive match1
PDFSearchMatchWholeWordMatch the whole word2

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.

Read matched text and surrounding context ​

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.

java
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.

Highlight and navigate through search results ​

When matched results need to be highlighted inside the viewer, the following methods are available.

Highlight a specific result ​

java
int pageIndex = 0;
int textRangeIndex = 0;
textSearcher.searchBegin(pageIndex, textRangeIndex);
readerView.invalidateAllChildren();

Go to the previous result ​

java
textSearcher.searchBackward();

Go to the next result ​

java
textSearcher.searchForward();

End the current search flow ​

java
textSearcher.cancelSearch();

Extract text from a page range ​

In addition to keyword search, CPDFPage and CPDFTextPage also support extracting text from a rectangular region. This is useful for:

  • page summary extraction
  • custom content analysis
  • fixed-area text reading

The following example defines a rectangle in page coordinates, then converts it into the coordinate range used for text extraction on the current page.

java
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());
}
kotlin
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.

Keep these points in mind ​

  • Full-text search is meant for locating keywords. Range extraction is meant for reading text from a defined area.
  • Before reading matched results, check whether the result collection is empty.
  • If the screen must render a search result list, it is useful to preserve the mapping between pageIndex and CPDFTextRange.
  • If the goal is to retrieve text that was manually selected in the viewer, see Get the Selected Content.