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 specific 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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/ocrInput.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
// Creates a new job instance
OCRJob ocrJob = new OCRJob(asset);
// Submit the job and gets the job result
String location = pdfServices.submit(ocrJob);
PDFServicesResponse<OCRResult> pdfServicesResponse = pdfServices.getJobResult(location, OCRResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/ocrOutput.pdf").toPath());
LOGGER.info("Saving asset at output/ocrOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}

OCR with specific 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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/ocrInput.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
// Create parameters for the job
OCRParams ocrParams = OCRParams.ocrParamsBuilder()
.withOCRLocale(OCRSupportedLocale.EN_US)
.withOCRType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
.build();
// Creates a new job instance
OCRJob ocrJob = new OCRJob(asset).setParams(ocrParams);
// Submit the job and gets the job result
String location = pdfServices.submit(ocrJob);
PDFServicesResponse<OCRResult> pdfServicesResponse = pdfServices.getJobResult(location, OCRResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/ocrWithOptionsOutput.pdf").toPath());
LOGGER.info("Saving asset at output/ocrWithOptionsOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} 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.