Applying OCR to a PDF document
Scanned documents arrive in many formats: image-based PDFs, multi-page TIFFs, single PNG or JPG scans, or even faxed exports. In every case the pages are pictures of text rather than selectable, searchable content. Legal firms receiving historical case files, healthcare teams handling medical records, and finance departments archiving older statements all share this problem: the information is visible but not searchable.
Applying OCR adds an invisible text layer that sits behind the page image. The visual appearance doesn’t change, but the text becomes selectable, searchable, and readable by assistive technologies.
This sample shows how to run OCR over every page of a document using Nutrient Java SDK and produce a searchable PDF as output. The input can be any document format the SDK supports. If the input isn’t already a PDF, the SDK converts it to PDF automatically when you create the editor.
Download sampleHow Nutrient helps
Nutrient Java SDK handles the full OCR pipeline behind a single method call. The SDK takes care of:
- Implicitly converting non-PDF inputs (images, multi-page TIFFs, Office documents) to PDF when the editor is created
- Rendering each PDF page to a bitmap at the resolution OCR needs
- Running text recognition with the configured languages
- Preserving reading order and text block orientation returned by the recognizer
- Placing an invisible, correctly positioned text layer over the original page content
You control the outcome through document settings, such as which OCR languages to use.
Preparing the project
Specify a package name and create the main class:
package io.nutrient.Sample;Import the classes used in the sample:
import io.nutrient.sdk.Document;import io.nutrient.sdk.editors.PdfEditor;
public class ApplyOcrToPdf {Running OCR on the whole document
The entry point opens the source document, configures the OCR language, and runs OCR across every page through PdfEditor.makeSearchable(). This sample passes an image-based PDF as input, but the same code handles raw images, multi-page TIFFs, or any other supported document format:
public static void main(String[] args) { try (Document document = Document.open("input_image_based.pdf")) { document.getSettings().getOcrSettings().setDefaultLanguages("eng");
PdfEditor editor = PdfEditor.edit(document); editor.makeSearchable();The call to setDefaultLanguages("eng") tells the recognizer which language models to load. Pass additional languages separated by + (for example "eng+deu") when you know the document contains more than one language. The default changes what the recognizer sees for ambiguous characters, so setting it to match the document content directly improves accuracy.
PdfEditor.edit(document) attaches an editor to the open document. If the document isn’t already a PDF, the SDK converts it to PDF at this step so the rest of the pipeline works on a uniform page representation. Calling editor.makeSearchable() loops through every page, runs OCR, and writes an invisible text layer on top of the existing page content. Any hidden text already present on a page is removed before the new layer is drawn, so re-running OCR doesn’t duplicate content.
Saving the result
Save the modified document to a new file and close the editor:
editor.saveAs("output.pdf"); editor.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } }}The try block uses try-with-resources, so the Document is closed automatically when the block exits, even if OCR throws. The catch clause surfaces any licensing, language-pack, or I/O issue raised by the SDK.
Conclusion
The workflow for OCR-ing a whole PDF is:
- Open the source document.
- Configure OCR languages on the document settings.
- Create a
PdfEditorfor the document. - Call
makeSearchable()to apply OCR to every page. - Save the result and close the editor.
The output is a standard PDF with an invisible text layer, so existing PDF viewers, search tools, and accessibility software can read it without any extra configuration.
For related workflows, refer to the Java SDK guides.