Skip to content
Guides

Text Search and Selection

When users perform a text search in a PDF document, the search results are typically highlighted to indicate matching text segments, and links are provided for easy navigation to the corresponding locations. With the text search and selection functionality offered by the ComPDFKit SDK, you can effortlessly implement this feature.

Text search enables users to input keywords throughout the entire PDF document to locate matching text.

The text search feature allows users to quickly pinpoint and retrieve information from large documents, enhancing document accessibility and search efficiency. This is particularly beneficial for workflows involving handling large documents, researching materials, or searching for specific information.

The steps for text search are as follows:

  1. Create a container for the search results set.
  2. Create CPDFTextPage and CPDFTextSearcher objects.
  3. Specify the CPDFTextPage object to be searched, the keywords for the search, search options, and the number of characters before and after the exact search result.
  4. Record the content of the temporary variable in the container each time a search result is found.

This example shows how to search specified text:

java
// Specify the search results collection.
List<CPDFTextRange> searchTextInfoList = new ArrayList<>();
ITextSearcher textSearcher = readerView.getTextSearcher();
// Specify the keyword.
String keywords = "compdfkit";
// Set the search keyword and options. If the default is case-insensitive and partial match, specify options to make it case-sensitive and perform a full-word match.
textSearcher.setSearchConfig(keywords, CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseSensitive);
// For-loop to iterate through all pages and search for the keyword.
for (int i = 0; i < document.getPageCount(); i++) {
  CPDFPage page = document.pageAtIndex(i);
  CPDFTextPage textPage = page.getTextPage();
  if ((null == textPage) || !textPage.isValid()) {
    continue;
  }
  final List<CPDFTextRange> searchPageContent = textSearcher.searchKeyword(i);
  if (searchPageContent.size() > 0) {
    // Save the search results to a collection.
    searchTextInfoList.addAll(searchPageContent);
  }
  page.close();
}
kotlin
// Specify the search results collection.
val searchTextInfoList = mutableListOf<CPDFTextRange>()
val textSearcher: ITextSearcher = readerView.getTextSearcher()
// Specify the keyword.
val keywords = "compdfkit"
// Set the search keyword and options. If the default is case-insensitive and partial match, 
// specify options to make it case-sensitive and perform a full-word match.
textSearcher.setSearchConfig(
  keywords,
  CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseSensitive
)
// For-loop to iterate through all pages and search for the keyword.
for (i in 0 until document.pageCount) {
  val page = document.pageAtIndex(i)
  val textPage = page.textPage
  if (null == textPage || !textPage.isValid) {
    continue
  }
  val searchPageContent: List<CPDFTextRange> = textSearcher.searchKeyword(i)
  if (searchPageContent.isNotEmpty()) {
    // Save the search results to a collection.
    searchTextInfoList.addAll(searchPageContent)
  }
  page.close()
}

Explanation of Search Settings

OptionDescriptionValue
CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseInsensitiveCase Insensitive0
CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseSensitiveCase Sensitive1
CPDFTextSearcher.PDFSearchOptions.PDFSearchMatchWholeWordMatch Whole Word2

Text Selection

The text content is stored in the CPDFPage object associated with the respective page. The CPDFPage object can be used to retrieve information about the text on a PDF page, such as individual characters, words, and text content within specified character ranges, or within specified boundaries.

By specifying a rectangular range (RectF), capture the text covered by this rectangle along with the position of the text field and store them in variables. This is done to simulate the action of selecting text by dragging the mouse or finger.

This example shows how to select specified text:

java
// Specify the page for selecting the text.
CPDFPage pdfPage = document.pageAtIndex(0);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
// Specify the rectangular range.
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
selectRect = pdfPage.convertRectFromPage(false, pdfPage.getSize().width(), pdfPage.getSize().height(), selectRect);
CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);

int len = textSelectionArr.length;
for (int i = 0; i < len; i++) {
    CPDFTextSelection textSelection = textSelectionArr[i];
    if (textSelection == null) {
        continue;
    }
  	// Retrieve the text content within the specified rectangular range.
    String text = pdfTextPage.getText(textSelection.getTextRange());
}
kotlin
// Specify the page for selecting the text.
val pdfPage = document.pageAtIndex(0)
val pdfTextPage = pdfPage.textPage
// Specify the rectangular range.
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) {
  // Retrieve the text content within the specified rectangular range.
  val text = pdfTextPage.getText(textSelection.textRange)
}