Text Search

PSPDFKit offers many different ways to search a document, including using a highly customizable UI component.

Searching via PSPDFViewController

In most cases, you’ll want to trigger a search inside PDFViewController. This can be done by calling search(for:options:sender:animated:). We’ll automatically anchor the popover to the search button or show a modal dialog for compact size classes like the iPhone. Options can take one parameter, .searchHeadless, which invokes a search but does not trigger any UI. This is great for unobtrusive highlighting:

Copy
1
2
let searchString = "Example Search Text"
pdfController.search(for: searchString, options: [.searchHeadless: true], sender: nil, animated: true)
Copy
1
2
NSString *searchString = @"Example Search Text";
[pdfController searchForString:searchString options:@{PSPDFPresentationOptionSearchHeadless: @YES} sender:nil animated:YES];

When a search result is selected, PDFViewController navigates to the page of the result and zooms into the text. PSPDFKit allows you to modify the scale of the zoom performed using the searchResultZoomScale property on the PDFConfiguration object. It defaults to PSPDFAutomaticSearchResultZoomScale, which enables automatic zoom scaling of the selected search result.

This automatic zoom can be disabled:

Copy
1
2
3
4
let controller = PDFController(document: document) {
    // Set `searchResultZoomScale` to `1` to disable zooming altogether.
    $0.searchResultZoomScale = 1.0
}
Copy
1
2
3
4
5
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    // Set `searchResultZoomScale` to `1` to disable zooming altogether.
    builder.searchResultZoomScale = 1.0;
}];
PSPDFViewController *controller = [[PSPDFViewController alloc] initWithDocument:document configuration:configuration];

Searching Manually

To search text inside a document, create an instance of TextSearch, passing in the loaded Document via its initializer. Searching can be triggered via calling search(for:), which will start a search on a background queue. Implement TextSearchDelegate on the receiving object and set the text search object’s delegate to your object to be notified of search result updates.

❗ Important: You need to retain the text search object while the search is running. Otherwise, any running search will automatically be canceled and your delegate will not get called.

Specify Search Options

Before triggering a search, you can configure various search options:

Changing these properties once a search operation is running is not supported and might result in unexpected behavior.

Highlighting Search Results

Search results, represented by SearchResult, offer the content (selection) as a TextBlock and a previewText. You can use these objects to represent your own search result list or to manually highlight parts of a page.

SearchHighlightViewManager manages the task of showing and hiding search results. It can be accessed via the searchHighlightViewManager property on PDFViewController, and it offers a way to both add (addHighlight(_:animated)) and clear (clearHighlightedSearchResults(animated:)) search results.

SearchHighlightView will be added on top of a visible PDFPageView for each search result. It can be customized via UIAppearance — see selectionBackgroundColor and cornerRadiusProportion.