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
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:
Create a container for the search results set.
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.Record the content of the temporary variable in the container each time a result is found.
This example shows how to search specified text:
// 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())
}
}
}
// 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
Option | Description | Value |
---|---|---|
CPDFSearchCaseInsensitive | Case Insensitive | 0 |
CPDFSearchCaseSensitive | Case Sensitive | 1 |
CPDFSearchMatchWholeWord | Match Whole Word | 2 |
CPDFSearchConsecutive | Will skip past the current match to look for the next match | 4 |
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:
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
}
- (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;
}