iOS
ComPDFKit PDF SDK
Guides

Text Search & Selection

 

Text Search

 

ComPDFKit PDF SDK offers developers an API for programmatic full-text search, as well as UI for searching and highlighting relevant matches.

 

- Asynchronously finds all instances of the specified string in the document, use function CPDFDocument::beginFindString:withOptions:, which returns immediately. It causes delegate methods to be called when searching begins and ends, on each search hit, and when the search proceeds to a new page

 

/**
 * Called when the beginFindString:withOptions: or findString:withOptions: method begins finding.
 */
- (void)documentDidBeginDocumentFind:(CPDFDocument *)document;
/**
 * Called when the beginFindString:withOptions: or findString:withOptions: method returns.
 */
- (void)documentDidEndDocumentFind:(CPDFDocument *)document;
/**
 * Called when a find operation begins working on a new page of a document.
 */
- (void)documentDidBeginPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
/**
 * Called when a find operation finishes working on a page in a document.
 */
- (void)documentDidEndPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
/**
 * Called when a string match is found in a document.
 *
 * @discussion To determine the string selection found, use the selection.
 */
- (void)documentDidFindMatch:(CPDFSelection *)selection;

 

- Synchronously finds all instances of the specified string in the document, use function CPDFDocument::findString:withOptions:.Each hit gets added to an NSArray object as a CPDFSelection object. If there are no hits, this method returns an empty array. Use this method when the complete search process will be brief and when you don’t need the flexibility or control offered by CPDFDocument::beginFindString:withOptions:.

 

- Cancels a search initiated with CPDFDocument::beginFindString:withOptions: ,use function CPDFDocument::cancelFindString.

 

- Highlighting Search Results, CPDFView offers a way to both add and clear search results, use function CPDFView::setHighlightedSelection:animated.

 

Text Selection

 

PDF text contents are stored in CPDFPage objects which are related to a specific page. CPDFPage class can be used to retrieve information about text in a PDF page, such as single character, single word, text content within specified character range or bounds and more.

 

How to get the text bounds on a page by 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;
}