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. Specify the CPDFSelection object to be searched, the keyword to be searched, search options, and the number of characters before and after the precise search results.

  3. Record the content of the temporary variable in the container each time a result is found.

This example shows how to search specified text:

swift
// Specify the container for storing the location and content of search results.
var rectArray = [CGRect]()
var stringArray = [String]()

// Set the search keyword and search options. The search options include case sensitivity (by default, it is not case-sensitive, and it is not a full word match).
if let resultArray = document.document?.find("searchText", with: .caseSensitive) as? [[CPDFSelection]] {
    // For loop to iterate through all pages and search for the keyword.
    for array in resultArray {
        for selection in array {
            // Save the search results to a collection.
            let rectValue = NSValue(cgRect: selection.bounds)
            rectArray.append(rectValue.cgRectValue)
            stringArray.append(selection.string())
        }
    }
}
objective-c
// Specify the container for storing the location and content of search results.
NSMutableArray *rectAraay = [[NSMutableArray alloc] init];
NSMutableArray *stringAraay = [[NSMutableArray alloc] init];

// Set the search keyword and search options. The search options include case sensitivity (by default, it is not case-sensitive, and it is not a full word match).
NSArray *resultArray = [document findString:@"searchText" withOptions:CPDFSearchCaseInsensitive];

// For loop to iterate through all pages and search for the keyword.
for (NSArray *array in resultArray) {
        for (CPDFSelection *selection in array) {
          // Save the search results to a collection.
            NSValue *rectValue = [NSValue valueWithCGRect:selection.bounds];
            [rectAraay addObject:rectValue];
            [stringAraay addObject:selection.string];
        }
}

Explanation of Search Settings

OptionDescriptionValue
CPDFSearchCaseInsensitiveCase Insensitive0
CPDFSearchCaseSensitiveCase Sensitive1
CPDFSearchMatchWholeWordMatch Whole Word2
CPDFSearchConsecutiveWill skip past the current match to look for the next match4

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:

swift
func selection(for page: CPDFPage, from fPoint: CGPoint, to tPoint: CGPoint) -> CPDFSelection? {
    let fCharacterIndex = page.characterIndex(at: fPoint)
    let tCharacterIndex = page.characterIndex(at: tPoint)
    let range = NSRange(location: fCharacterIndex, length: tCharacterIndex - fCharacterIndex + 1)
    let selection = page.selection(for: range)
    return selection
}
objective-c
- (CPDFSelection *)selectionForPage:(CPDFPage *)page fromPoint:(CGPoint)fPoint toPoint:(CGPoint)tPoint {
		NSInteger fCharacterIndex = [page characterIndexAtPoint:fPoint];
		NSInteger tCharacterIndex = [page characterIndexAtPoint:tPoint];
  	NSRange range = NSMakeRange(fCharacterIndex, tCharacterIndex - fCharacterIndex + 1);
  	CPDFSelection *selection = [page selectionForRange:range];
  	return selection;
}