Edit in GitHubLog an issue

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.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.ocrpdf.OcrPDF
4
5 public class OcrPDF {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDF.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 OCROperation ocrOperation = OCROperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
25 ocrOperation.setInput(source);
26
27 // Execute the operation
28 FileRef result = ocrOperation.execute(executionContext);
29
30 // Save the result at the specified location
31 result.saveAs("output/ocrOutput.pdf");
32
33 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
34 LOGGER.error("Exception encountered while executing operation", ex);
35 }
36 }
37 }

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.
Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java Dexec.mainClass=com.adobe.pdfservices.operation.samples.ocrpdf.OcrPDFWithOptions
4
5 public class OcrPDFWithOptions {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDFWithOptions.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 OCROperation ocrOperation = OCROperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
25 ocrOperation.setInput(source);
26
27 // Build OCR options from supported locales and OCR-types and set them into the operation
28 OCROptions ocrOptions = OCROptions.ocrOptionsBuilder()
29 .withOCRLocale(OCRSupportedLocale.EN_US)
30 .withOCRType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
31 .build();
32 ocrOperation.setOptions(ocrOptions);
33
34 // Execute the operation
35 FileRef result = ocrOperation.execute(executionContext);
36
37 // Save the result at the specified location
38 result.saveAs("output/ocrWithOptionsOutput.pdf");
39
40 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
41 LOGGER.error("Exception encountered while executing operation", ex);
42 }
43 }
44 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.