Search for Text in PDFs on iOS
PSPDFKit offers many different ways to search a document, including using a highly customizable UI component. PSPDFKit also offers a component that enables efficient search across a set of documents. To learn more about that, check out the Indexed Full-Text Search guide.
Searching Using the User Interface
In most cases, you’ll want to trigger a search inside PDFViewController
. This can be done by calling search(for:options:sender:animated:)
(opens in a new tab). PSPDFKit will 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
(opens in a new tab), which invokes a search but doesn’t trigger any UI. This is great for unobtrusive highlighting:
let searchString = "Example Search Text"pdfController.search(for: searchString, options: [.searchHeadless: true], sender: nil, animated: true)
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 in to the text. PSPDFKit allows you to modify the scale of the zoom performed using the searchResultZoomScale
(opens in a new tab) property on the PDFConfiguration
object. It defaults to PSPDFAutomaticSearchResultZoomScale
(opens in a new tab), which enables automatic zoom scaling of the selected search result.
This automatic zoom can be disabled:
let controller = PDFController(document: document) { // Set `searchResultZoomScale` to `1` to disable zooming altogether. $0.searchResultZoomScale = 1.0}
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 Programmatically
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:)
(opens in a new tab), 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.
You need to retain the text search object while the search is running. Otherwise, any running search will automatically be canceled and your delegate won’t get called.
Specifying the Search Options
Before triggering a search, you can configure various search options:
comparisonOptions
(opens in a new tab) — Use this for regular expression (regex) search and to control case and diacritic insensitivity.previewRange
(opens in a new tab)searchableAnnotationTypes
(opens in a new tab)maximumNumberOfSearchResults
(opens in a new tab)
Changing these properties once a search operation is running isn’t supported and might result in unexpected behavior.
To learn more about searching annotations and the searchableAnnotationTypes
(opens in a new tab) option, see the Annotation Search guide.
Highlighting Search Results
Search results, represented by SearchResult
(opens in a new tab), offer the content (selection) as a TextBlock
and a previewText
(opens in a new tab). You can use these objects to represent your own search result list or to manually highlight parts of a page.
SearchHighlightViewManager
(opens in a new tab) manages the task of showing and hiding search results. It can be accessed via the searchHighlightViewManager
(opens in a new tab) property on PDFViewController
, and it offers a way to both add (addHighlight(_:animated)
(opens in a new tab)) and clear (clearHighlightedSearchResults(animated:)
(opens in a new tab)) search results.
SearchHighlightView
(opens in a new tab) will be added on top of a visible PDFPageView
(opens in a new tab) for each search result. It can be customized via UIAppearance
— see selectionBackgroundColor
(opens in a new tab) and cornerRadiusProportion
(opens in a new tab).