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
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
5public 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 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/ocrInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Creates a new job instance
24 OCRJob ocrJob = new OCRJob(asset);
25
26 // Submit the job and gets the job result
27 String location = pdfServices.submit(ocrJob);
28 PDFServicesResponse<OCRResult> pdfServicesResponse = pdfServices.getJobResult(location, OCRResult.class);
29
30 // Get content from the resulting asset(s)
31 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
32 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
33
34 // Creates an output stream and copy stream asset's content to it
35 Files.createDirectories(Paths.get("output/"));
36 OutputStream outputStream = Files.newOutputStream(new File("output/ocrOutput.pdf").toPath());
37 LOGGER.info("Saving asset at output/ocrOutput.pdf");
38 IOUtils.copy(streamAsset.getInputStream(), outputStream);
39 outputStream.close();
40 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
41 LOGGER.error("Exception encountered while executing operation", ex);
42 }
43 }
44}

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
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 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDFWithOptions.class);
8
9 public static void main(String[] args) {
10
11 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/ocrInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Create parameters for the job
24 OCRParams ocrParams = OCRParams.ocrParamsBuilder()
25 .withOCRLocale(OCRSupportedLocale.EN_US)
26 .withOCRType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
27 .build();
28
29 // Creates a new job instance
30 OCRJob ocrJob = new OCRJob(asset).setParams(ocrParams);
31
32 // Submit the job and gets the job result
33 String location = pdfServices.submit(ocrJob);
34 PDFServicesResponse<OCRResult> pdfServicesResponse = pdfServices.getJobResult(location, OCRResult.class);
35
36 // Get content from the resulting asset(s)
37 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
38 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
39
40 // Creates an output stream and copy stream asset's content to it
41 Files.createDirectories(Paths.get("output/"));
42 OutputStream outputStream = Files.newOutputStream(new File("output/ocrWithOptionsOutput.pdf").toPath());
43 LOGGER.info("Saving asset at output/ocrWithOptionsOutput.pdf");
44 IOUtils.copy(streamAsset.getInputStream(), outputStream);
45 outputStream.close();
46 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
47 LOGGER.error("Exception encountered while executing operation", ex);
48 }
49 }
50 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.