Applying OCR to a PDF page
Running OCR on a single page is useful when only part of a document needs recognition, or when pages are processed on demand as a larger workflow progresses. Examples include scanning the cover page of a batch to extract a reference number, applying OCR only to pages flagged during triage, or incrementally processing a long document without reprocessing pages that are already searchable.
Applying OCR at the page level adds an invisible text layer to just that page. The rest of the document is untouched, so the operation is fast and idempotent per page.
This sample shows how to run OCR on a single page of a document using Nutrient Java SDK and save the result. The input can be any document format the SDK supports, such as an image-based PDF, a multi-page TIFF, or a single image. 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 exposes the same OCR pipeline at the page level. Behind a single method call the SDK:
- Implicitly converts non-PDF inputs (images, multi-page TIFFs, Office documents) to PDF when the editor is created
- Renders just the target page to a bitmap at the resolution OCR needs
- Runs text recognition with the configured languages
- Preserves reading order and text block orientation returned by the recognizer
- Places an invisible, correctly positioned text layer over the original page content
Other pages in the document aren’t touched.
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;import io.nutrient.sdk.editors.pdf.pages.PdfPage;import io.nutrient.sdk.editors.pdf.pages.PdfPageCollection;
public class ApplyOcrToPdfPage {Running OCR on a single page
The entry point opens the source document, configures the OCR language, then runs OCR only on the first page. This sample passes an image-based PDF as input, but the same code handles raw images 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); PdfPageCollection pages = editor.getPageCollection();
PdfPage page = pages.getFirst(); page.makeSearchable();The call to setDefaultLanguages("eng") tells the recognizer which language models to load. Pass additional languages separated by + (for example "eng+deu") when the page contains more than one language.
PdfEditor.edit(document) attaches an editor to the open document. If the input isn’t already a PDF, the SDK converts it to PDF at this step. editor.getPageCollection().getFirst() then returns the first page of the resulting PDF as a PdfPage. Calling page.makeSearchable() runs OCR on that page and writes an invisible text layer on top of it. Any hidden text already present on the page is removed before the new layer is drawn. Other pages in the document are left unchanged.
To target a different page, use the indexer on the page collection (for example pages.get(2) for the third page) and call makeSearchable() on that page instead.
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 single PDF page is:
- Open the source document.
- Configure OCR languages on the document settings.
- Create a
PdfEditorfor the document. - Get the target page from
editor.getPageCollection(). - Call
makeSearchable()on that page. - Save the result and close the editor.
Only the targeted page gains the invisible text layer. The rest of the document is bit-for-bit identical to the input.
For related workflows, refer to the Java SDK guides.