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:)
. 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
, 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
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:
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:)
, 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
— Use this for regular expression (regex) search and to control case and diacritic insensitivity.
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
option, please see the Annotation Search guide.
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
.