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 search and select 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. Specify the storage location for search results and the content container.
  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. Use a temporary variable to store the context of the search results.
  5. Record the contents of the temporary variable in the container whenever a search result is found.

This example shows how to search specified text:

C#
// instructing the storage location and content variables of search results
List<Rect> rectList = new List<Rect>(); 
List<string> stringList = new List<string>();

// Specify the `CPDFTextPage` object to be searched, the keywords to be searched, the search options for case sensitivity and whole word matching, and the starting index for recording search results.
CPDFTextPage textPage = myCPDFPage.GetTextPage();
CPDFTextSearcher searcher = new CPDFTextSearcher();
if (searcher.FindStart(textPage, searchKeywords,  C_Search_Options.Search_Case_Sensitive | C_Search_Options.Search_Match_Whole_Word, 0))
{
// Use temporary variables to store the context and position of search results, and record them in the container whenever a result is found.
	CRect textRect = new CRect();
	string textContent = "";
	while (searcher.FindNext(page, textPage, ref textRect, ref textContent, ref findIndex))
	{
		// Record the content of the temporary variables in the container whenever a search result is found.
		stringList.Add(textContent);
		rectList.Add(new Rect(textRect.left, textRect.top, textRect.width(), textRect.height()));
	}
}

Explanation of Search Settings

OptionDescriptionValue
C_Search_Options.Search_Case_InsensitiveCase Insensitive0
C_Search_Options.Search_Case_SensitiveCase Sensitive1
C_Search_Options.Search_Match_Whole_WordMatch 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.

Create a rectangle by specifying two diagonal coordinates (Points), and capture the text covered by the rectangle along with the position of the text area. This simulates the action of dragging the mouse or finger to select text.

This example shows how to select specified text:

C#
void SelectForPage(CPDFPage page, Point fromPoint, Point toPoint, ref List<Rect> rects, ref string textContent)
{
    CPDFTextPage textPage = page.GetTextPage();
    // Form a rectangle by specifying two diagonal coordinate points and retrieve the text content within the rectangular space defined by these two points.
    textContent = textPage.GetSelectText(fromPoint, toPoint);
    // Create a rectangle by specifying two diagonal coordinate points and obtain the position of the rectangular space between these two points, allowing a deviation of (10,10).
    rects = textPage.GetCharsRectAtPos(fromPoint, toPoint, new Point(10, 10));
}