Edit in GitHubLog an issue

Quickstart for PDF Accessibility Auto-Tag API (Java)

To get started using Adobe PDF Accessibility Auto-Tag API, let's walk through a simple scenario - taking an input PDF document and running PDF Accessibility Auto-Tag API against it. Once the PDF has been tagged, we'll provide the document with tags and optionally, a report file. In this guide, we will walk you through the complete process for creating a program that will accomplish this task.

Prerequisites

To complete this guide, you will need:

  • Java - Java 11 or higher is required.
  • Maven
  • An Adobe ID. If you do not have one, the credential setup will walk you through creating one.
  • A way to edit code. No specific editor is required for this guide.

Step One: Getting credentials

1) To begin, open your browser to https://acrobatservices.adobe.com/dc-integration-creation-app-cdn/main.html?api=pdf-accessibility-auto-tag-api. If you are not already logged in to Adobe.com, you will need to sign in or create a new user. Using a personal email account is recommend and not a federated ID.

Sign in

2) After registering or logging in, you will then be asked to name your new credentials. Use the name, "New Project".

3) Change the "Choose language" setting to "Java".

4) Also note the checkbox by, "Create personalized code sample." This will include a large set of samples along with your credentials. These can be helpful for learning more later.

5) Click the checkbox saying you agree to the developer terms and then click "Create credentials."

Project setup

6) After your credentials are created, they are automatically downloaded:

alt

Step Two: Setting up the project

1) In your Downloads folder, find the ZIP file with your credentials: PDFServicesSDK-JavaSamples.zip. If you unzip that archive, you will find a folder of samples and the pdfservices-api-credentials.json file.

alt

2) Take pdfservices-api-credentials.json file and place it in a new directory.

3) In this directory, create a new file named pom.xml and copy the following content:

Copied to your clipboard
1<?xml version="1.0" encoding="UTF-8"?>
2
3<project xmlns="http://maven.apache.org/POM/4.0.0"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6 <modelVersion>4.0.0</modelVersion>
7
8 <groupId>com.adobe.documentservices</groupId>
9 <artifactId>pdfservices-sdk-autotag-guide</artifactId>
10 <version>1</version>
11
12 <name>PDF Services Java SDK Samples</name>
13
14 <properties>
15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16 <maven.compiler.source>11</maven.compiler.source>
17 <maven.compiler.target>11</maven.compiler.target>
18 <pdfservices.sdk.version>4.0.0</pdfservices.sdk.version>
19 </properties>
20
21 <dependencies>
22
23 <dependency>
24 <groupId>com.adobe.documentservices</groupId>
25 <artifactId>pdfservices-sdk</artifactId>
26 <version>${pdfservices.sdk.version}</version>
27 </dependency>
28
29 <!-- log4j2 dependency to showcase the use of log4j2 with slf4j API-->
30 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
31 <dependency>
32 <groupId>org.apache.logging.log4j</groupId>
33 <artifactId>log4j-slf4j-impl</artifactId>
34 <version>2.21.1</version>
35 </dependency>
36 </dependencies>
37
38 <build>
39 <plugins>
40 <plugin>
41 <groupId>org.apache.maven.plugins</groupId>
42 <artifactId>maven-compiler-plugin</artifactId>
43 <version>3.8.0</version>
44 <configuration>
45 <source>${maven.compiler.source}</source>
46 <target>${maven.compiler.target}</target>
47 </configuration>
48 </plugin>
49 <plugin>
50 <groupId>org.codehaus.mojo</groupId>
51 <artifactId>exec-maven-plugin</artifactId>
52 <version>1.5.0</version>
53 <executions>
54 <execution>
55 <goals>
56 <goal>java</goal>
57 </goals>
58 </execution>
59 </executions>
60 </plugin>
61 </plugins>
62 </build>
63</project>

This file will define what dependencies we need and how the application will be built.

Our application will take a PDF, Adobe_Accessibility_Auto_Tag_API_Sample.pdf (downloadable from here) and tag its contents. The results will be saved in the directory /output.

4) In your editor, open the directory where you previously copied the credentials, and create a new directory, src/main/java. In that directory, create AutotagPDF.java.

Now you're ready to begin coding.

Step Three: Creating the application

1) We'll begin by including our required dependencies:

Copied to your clipboard
1import com.adobe.pdfservices.operation.PDFServices;
2import com.adobe.pdfservices.operation.PDFServicesMediaType;
3import com.adobe.pdfservices.operation.PDFServicesResponse;
4import com.adobe.pdfservices.operation.auth.Credentials;
5import com.adobe.pdfservices.operation.auth.ServicePrincipalCredentials;
6import com.adobe.pdfservices.operation.exception.SdkException;
7import com.adobe.pdfservices.operation.exception.ServiceApiException;
8import com.adobe.pdfservices.operation.exception.ServiceUsageException;
9import com.adobe.pdfservices.operation.io.Asset;
10import com.adobe.pdfservices.operation.io.StreamAsset;
11import com.adobe.pdfservices.operation.pdfjobs.jobs.AutotagPDFJob;
12import com.adobe.pdfservices.operation.pdfjobs.result.AutotagPDFResult;
13import org.apache.commons.io.IOUtils;
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
17import java.io.File;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.OutputStream;
21import java.nio.file.Files;
22import java.nio.file.Paths;

2) Now let's define our main class:

Copied to your clipboard
1public class AutotagPDF {
2
3 private static final Logger LOGGER = LoggerFactory.getLogger(AutotagPDF.class);
4
5 public static void main(String[] args) {
6
7 }
8}

3) Set the environment variables PDF_SERVICES_CLIENT_ID and PDF_SERVICES_CLIENT_SECRET by running the following commands and replacing placeholders YOUR CLIENT ID and YOUR CLIENT SECRET with the credentials present in pdfservices-api-credentials.json file:

  • Windows:

    • set PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • set PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>
  • MacOS/Linux:

    • export PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • export PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>

4) Next, we can create our credentials and PDFServices instance:

Copied to your clipboard
1// Initial setup, create credentials instance
2Credentials credentials = new ServicePrincipalCredentials(
3 System.getenv("PDF_SERVICES_CLIENT_ID"),
4 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
5
6// Creates a PDF Services instance
7PDFServices pdfServices = new PDFServices(credentials);

5) Now, let's upload the asset:

Copied to your clipboard
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());

We define input stream for the PDF that will be tagged. (You can download the source we used here.) In a real application, these values would be typically be dynamic. Then we upload the content of input stream and specify the input media type as PDF.

6) Now, let's create the job and parameters:

Copied to your clipboard
1// Create parameters for the job
2AutotagPDFParams autotagPDFParams = AutotagPDFParams.autotagPDFParamsBuilder().generateReport().shiftHeadings().build();
3
4// Creates a new job instance
5AutotagPDFJob autotagPDFJob = new AutotagPDFJob(asset).setParams(autotagPDFParams);

This set of code defines what we're doing (an Auto-Tag operation), it defines parameters for the Auto-Tag job. PDF Accessibility Auto-Tag API has a few different options, but in this example, we're simply asking for a basic tagging operation, which returns the tagged PDF document and an XLSX report of the document.

7) The next code block submits the job and gets the job result:

Copied to your clipboard
1// Submit the job and get the job result
2String location = pdfServices.submit(autotagPDFJob);
3PDFServicesResponse<AutotagPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, AutotagPDFResult.class);
4
5// Get content from the resulting asset(s)
6Asset resultAsset = pdfServicesResponse.getResult().getTaggedPDF();
7Asset resultAssetReport = pdfServicesResponse.getResult().getReport();
8StreamAsset streamAsset = pdfServices.getContent(resultAsset);
9StreamAsset streamAssetReport = pdfServices.getContent(resultAssetReport);

8) The next code block saves the result at the specified location:

Copied to your clipboard
1// Creating output streams and copying stream assets' content to it
2OutputStream outputStream = Files.newOutputStream(new File("output/autotag-tagged.pdf").toPath());
3OutputStream outputStreamReport = Files.newOutputStream(new File("output/autotag-report.xlsx").toPath());
4IOUtils.copy(streamAsset.getInputStream(), outputStream);
5IOUtils.copy(streamAssetReport.getInputStream(), outputStreamReport);

Example running in the command line

Here's the complete application (src/main/java/AutotagPDF.java):

Copied to your clipboard
1import com.adobe.pdfservices.operation.PDFServices;
2import com.adobe.pdfservices.operation.PDFServicesMediaType;
3import com.adobe.pdfservices.operation.PDFServicesResponse;
4import com.adobe.pdfservices.operation.auth.Credentials;
5import com.adobe.pdfservices.operation.auth.ServicePrincipalCredentials;
6import com.adobe.pdfservices.operation.exception.SDKException;
7import com.adobe.pdfservices.operation.exception.ServiceApiException;
8import com.adobe.pdfservices.operation.exception.ServiceUsageException;
9import com.adobe.pdfservices.operation.io.Asset;
10import com.adobe.pdfservices.operation.io.StreamAsset;
11import com.adobe.pdfservices.operation.pdfjobs.jobs.AutotagPDFJob;
12import com.adobe.pdfservices.operation.pdfjobs.params.autotag.AutotagPDFParams;
13import com.adobe.pdfservices.operation.pdfjobs.result.AutotagPDFResult;
14import org.apache.commons.io.IOUtils;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18import java.io.File;
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.OutputStream;
22import java.nio.file.Files;
23import java.nio.file.Paths;
24
25public class AutotagPDF {
26 // Initialize the logger
27 private static final Logger LOGGER = LoggerFactory.getLogger(AutotagPDF.class);
28
29 public static void main(String[] args) {
30 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/Adobe_Accessibility_Auto_Tag_API_Sample.pdf").toPath())) {
31 // Initial setup, create credentials instance
32 Credentials credentials = new ServicePrincipalCredentials(
33 System.getenv("PDF_SERVICES_CLIENT_ID"),
34 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
35
36 // Creates a PDF Services instance
37 PDFServices pdfServices = new PDFServices(credentials);
38
39 // Creates an asset(s) from source file(s) and upload
40 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
41
42 // Create parameters for the job
43 AutotagPDFParams autotagPDFParams = AutotagPDFParams.autotagPDFParamsBuilder().generateReport().shiftHeadings().build();
44
45 // Creates a new job instance
46 AutotagPDFJob autotagPDFJob = new AutotagPDFJob(asset).setParams(autotagPDFParams);
47
48 // Submit the job and gets the job result
49 String location = pdfServices.submit(autotagPDFJob);
50 PDFServicesResponse<AutotagPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, AutotagPDFResult.class);
51
52 // Get content from the resulting asset(s)
53 Asset resultAsset = pdfServicesResponse.getResult().getTaggedPDF();
54 Asset resultAssetReport = pdfServicesResponse.getResult().getReport();
55 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
56 StreamAsset streamAssetReport = pdfServices.getContent(resultAssetReport);
57
58 // Creating output streams and copying stream assets' content to it
59 Files.createDirectories(Paths.get("output/"));
60 OutputStream outputStream = Files.newOutputStream(new File("output/autotag-tagged.pdf").toPath());
61 OutputStream outputStreamReport = Files.newOutputStream(new File("output/autotag-report.xlsx").toPath());
62 LOGGER.info("Saving asset at output/autotag-tagged.pdf");
63 LOGGER.info("Saving asset at output/autotag-report.xlsx");
64 IOUtils.copy(streamAsset.getInputStream(), outputStream);
65 IOUtils.copy(streamAssetReport.getInputStream(), outputStreamReport);
66 outputStream.close();
67 outputStreamReport.close();
68 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
69 LOGGER.error("Exception encountered while executing operation", ex);
70 }
71 }
72}

Next Steps

Now that you've successfully performed your first operation, review the documentation for many other examples and reach out on our forums with any questions. Also remember the samples you downloaded while creating your credentials also have many demos.

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.