Edit in GitHubLog an issue

OCR PDF

Use built-in optical character recognition (OCR) to convert images to text and enable fully text searchable documents for archiving and creation of searchable indexes.

Rest API

See our public API Reference for OCR PDF

Text recognition (OCR)

Optical character recognition (OCR) converts images to text so that you and your users can fully interact with the PDF file. After performing OCR, the PDF may be fully editable and searchable. The input format must be application/pdf.

This sample defaults to the en-us locale. For other languages, see OCR with explicit language.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.ocrpdf.OcrPDF
public class OcrPDF {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDF.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance.
Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
.withClientId("PDF_SERVICES_CLIENT_ID")
.withClientSecret("PDF_SERVICES_CLIENT_SECRET")
.build();
//Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.create(credentials);
OCROperation ocrOperation = OCROperation.createNew();
// Set operation input from a source file.
FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
ocrOperation.setInput(source);
// Execute the operation
FileRef result = ocrOperation.execute(executionContext);
// Save the result at the specified location
result.saveAs("output/ocrOutput.pdf");
} catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}

OCR with explicit language

You can perform OCR on files in other languages, including German, French, Danish, and other languages. Refer to OCRSupportedLocale and OCRSupportedType in the API docs for a list of supported OCR locales and OCR types.

As shown in the OcrPDFWithOptions sample, when you make a PDF file searchable, you specify both the locale (language) and the type. There are two types which produce a different result:

  • One type ensures that text is searchable and selectable, but modifies the original image during the cleanup process (for example, deskews it) before placing an invisible text layer over it. This type removes unwanted artifacts and may result in a more readable document in some scenarios.
  • The second (EXACT) type, also overlays a searchable text layer over the original image, but in this case, the original image is unchanged. This type produces maximum fidelity to the original image.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java Dexec.mainClass=com.adobe.pdfservices.operation.samples.ocrpdf.OcrPDFWithOptions
public class OcrPDFWithOptions {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDFWithOptions.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance.
Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()
.withClientId("PDF_SERVICES_CLIENT_ID")
.withClientSecret("PDF_SERVICES_CLIENT_SECRET")
.build();
//Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.create(credentials);
OCROperation ocrOperation = OCROperation.createNew();
// Set operation input from a source file.
FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
ocrOperation.setInput(source);
// Build OCR options from supported locales and OCR-types and set them into the operation
OCROptions ocrOptions = OCROptions.ocrOptionsBuilder()
.withOCRLocale(OCRSupportedLocale.EN_US)
.withOCRType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
.build();
ocrOperation.setOptions(ocrOptions);
// Execute the operation
FileRef result = ocrOperation.execute(executionContext);
// Save the result at the specified location
result.saveAs("output/ocrWithOptionsOutput.pdf");
} catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.