How to perform OCR on a PDF in iOS
This guide explains how to use the Nutrient iOS OCR library to perform optical character recognition (OCR) on a PDF.
API overview
To access the OCR API and related features, integrate the OCR library into your project. The Nutrient iOS OCR library provides the following key API entry points for OCR:
-
performOCROnPages(at:options:)
— Performs OCR on specific page ranges. -
ProcessorOCROptions.init(language:)
— Specifies recognition languages to suit your document needs.
Performing OCR on a PDF
To perform OCR on a PDF document, follow the steps below:
-
Create a processor configuration and provide the document.
-
Use the
ProcessorOCROptions
method to specify the language for detecting text within the document. -
Use the
performOCROnPages(at:options:)
method to specify the pages for OCR processing.
Example code snippet
let document: Document = ... guard let processorConfiguration = Processor.Configuration(document: document) else { return } // Mark the processor to perform OCR on all document pages and detect text in English. processorConfiguration.performOCROnPages(at: IndexSet(0..<IndexSet.Element(document.pageCount)), options: ProcessorOCROptions(language: .english)) let processor = Processor(configuration: processorConfiguration, securityOptions: nil) let ocrURL: URL = ... // Writeable URL. DispatchQueue.global(qos: .userInitiated).async { do { // This performs the actual OCR and generates the new document at the provided URL. try processor.write(toFileURL: ocrURL) } catch { // Handle error. } DispatchQueue.main.async { let ocrDocument = Document(url: ocrURL) } }
PSPDFDocument *document = ... PSPDFProcessorConfiguration *processorConfiguration = [[PSPDFProcessorConfiguration alloc] initWithDocument:document]; // Mark the processor to perform OCR on all document pages and detect text in English. [processorConfiguration performOCROnPagesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, document.pageCount-1)] options:[[PSPDFProcessorOCROptions alloc] initWithLanguage:PSPDFOCRLanguageEnglish]]; PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:processorConfiguration securityOptions:nil]; NSURL *ocrURL = ... // Writeable URL. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ NSError *error; // This performs the actual OCR and generates the new document at the provided URL. [processor writeToFileURL:ocrURL error:&error]; if (error) { // Handle error. } dispatch_async(dispatch_get_main_queue(), ^{ PSPDFDocument *ocrDocument = [[PSPDFDocument alloc] initWithURL:ocrURL]; }); });
OCR works even if the document already contains real text objects. The existing content remains intact, and the newly detected text coexists with it.
The OCR process runs on a background thread to prevent blocking the main thread. Since OCR can take time, always execute it asynchronously.
Before and after OCR processing
Before OCR (scanned PDF)

After OCR (searchable and selectable text)

The example screenshots above are taken from an iPhone simulator.
Performing OCR on images
To perform OCR on an image, follow the steps below:
-
Convert the image into a PDF file as described in the image-to-PDF conversion guide.
-
Perform OCR on the converted PDF file so that the textual information is extracted out of the image. For more information, refer to the converting a scan into a searchable PDF guide.
-
Extract recognized text using
TextParser
. Use it to retrieve text blocks, words, or glyphs from the processed PDF. For more information, refer to the extract text from PDFs on iOS guide.
To view the complete code from the process above, refer to the convert images to text on iOS guide.
Managing OCR language bundles
The OCR feature includes separate trained data models for each supported language. To minimize app size, these models aren’t included by default and must be manually added based on your needs.
-
Add only the required language bundles to your project.
-
If you manually integrated the Nutrient iOS OCR library, drag the
.bundle
files into the Copy Bundle Resources build phase in your app.
For detailed steps, refer to the getting started guide.