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 8 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 README file, your private key, and a folder of samples:

alt

2) We need two things from this download. The private.key file (as shown in the screenshot above, and the pdfservices-api-credentials.json file found in the samples directory:

alt

3) Take these two files and place them in a new directory.

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

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>1.8</maven.compiler.source>
17 <maven.compiler.target>1.8</maven.compiler.target>
18 <pdfservices.sdk.version>3.3.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.17.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.apache.maven.plugins</groupId>
51 <artifactId>maven-shade-plugin</artifactId>
52 <version>3.2.4</version>
53 <configuration>
54 <filters>
55 <filter>
56 <artifact>*:*</artifact>
57 <excludes>
58 <exclude>META-INF/*.SF</exclude>
59 <exclude>META-INF/*.DSA</exclude>
60 <exclude>META-INF/*.RSA</exclude>
61 </excludes>
62 </filter>
63 </filters>
64 </configuration>
65 <executions>
66 <execution>
67 <phase>package</phase>
68 <goals>
69 <goal>shade</goal>
70 </goals>
71 </execution>
72 </executions>
73 </plugin>
74 <plugin>
75 <groupId>org.apache.maven.plugins</groupId>
76 <artifactId>maven-jar-plugin</artifactId>
77 <version>3.0.2</version>
78 <configuration>
79 <archive>
80 <manifest>
81 <addClasspath>true</addClasspath>
82 <classpathPrefix>lib/</classpathPrefix>
83 <mainClass>ExtractTextInfoFromPDF</mainClass>
84 </manifest>
85 </archive>
86 </configuration>
87 </plugin>
88 <plugin>
89 <groupId>org.codehaus.mojo</groupId>
90 <artifactId>exec-maven-plugin</artifactId>
91 <version>1.5.0</version>
92 <executions>
93 <execution>
94 <goals>
95 <goal>java</goal>
96 </goals>
97 </execution>
98 </executions>
99 </plugin>
100 </plugins>
101 </build>
102</project>

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

Our application will take a PDF, Adobe Accesibility Auto-Tag API Sample.pdf (downloadable from here) and tag its contents. The results will be saved in a given directory /output/AutotagPDF.

5) 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.ExecutionContext;
2import com.adobe.pdfservices.operation.auth.Credentials;
3import com.adobe.pdfservices.operation.exception.SdkException;
4import com.adobe.pdfservices.operation.exception.ServiceApiException;
5import com.adobe.pdfservices.operation.exception.ServiceUsageException;
6import com.adobe.pdfservices.operation.io.FileRef;
7import com.adobe.pdfservices.operation.io.autotag.AutotagPDFOutput;
8import com.adobe.pdfservices.operation.pdfops.AutotagPDFOperation;
9import com.adobe.pdfservices.operation.pdfops.options.autotag.AutotagPDFOptions;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import java.io.IOException;
14import java.time.LocalDateTime;
15import java.time.format.DateTimeFormatter;

2) Now let's define our main class:

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

3) Now let's define our input and output:

Copied to your clipboard
1String inputFile = "./Adobe Extract API Sample.pdf";
2
3String outputPath = "./output/AutotagPDF/";
4Files.deleteIfExists(Paths.get(outputPath));
5
6String taggedPDF = outputPath + inputPDF +"-tagged-pdf.pdf";
7String taggingReport = outputPath + inputPDF +"-tagging-report.xlsx";
8

This defines what our output directory will be and optionally deletes it if it already exists. Then we define what PDF will be tagged. (You can download the source we used here.) In a real application, these values would be typically be dynamic.

4) Next, we can create our credentials and use them:

Copied to your clipboard
1// Initial setup, create credentials instance.
2Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
3 .fromFile("pdfservices-api-credentials.json")
4 .build();
5
6// Create an ExecutionContext using credentials.
7ExecutionContext executionContext = ExecutionContext.create(credentials);

5) Now, let's create the operation:

Copied to your clipboard
1AutotagPDFOperation autotagPDFOperation = AutotagPDFOperation.createNew();
2
3// Build AutotagPDFOptions options and set them into the operation
4AutotagPDFOptions autotagPDFOptions = AutotagPDFOptions.autotagPDFOptionsBuilder()
5 .shiftHeadings()
6 .generateReport()
7 .build();
8autotagPDFOperation.setOptions(autotagPDFOptions);

This set of code defines what we're doing (an Extract operation), points to our local file and specifies the input is a PDF, and then defines options for the Extract call. PDF Extract API has a few different options, but in this example, we're simply asking for the most basic of extractions, the textual content of the document.

6) The next code block executes the operation:

Copied to your clipboard
1// Execute the operation
2AutotagPDFOutput result = autotagPDFOperation.execute(executionContext);
3
4// Save the tagged PDF output at the specified location
5autotagPDFOutput.getTaggedPDF().saveAs(taggedPDF);
6
7// Save the tagging report output at the specified location
8autotagPDFOutput.getReport().saveAs(taggingReport);

Example running in the command line

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

Copied to your clipboard
1import com.adobe.pdfservices.operation.ExecutionContext;
2import com.adobe.pdfservices.operation.auth.Credentials;
3import com.adobe.pdfservices.operation.exception.SdkException;
4import com.adobe.pdfservices.operation.exception.ServiceApiException;
5import com.adobe.pdfservices.operation.exception.ServiceUsageException;
6import com.adobe.pdfservices.operation.io.FileRef;
7import com.adobe.pdfservices.operation.io.autotag.AutotagPDFOutput;
8import com.adobe.pdfservices.operation.pdfops.AutotagPDFOperation;
9import com.adobe.pdfservices.operation.pdfops.options.autotag.AutotagPDFOptions;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import java.io.IOException;
14import java.time.LocalDateTime;
15import java.time.format.DateTimeFormatter;
16
17public class AutotagPDF {
18
19 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AutotagPDF.class);
20
21 public static void main(String[] args) {
22
23 try {
24
25 String inputFile = "./Adobe Extract API Sample.pdf";
26
27 String outputPath = "./output/AutotagPDF/";
28 Files.deleteIfExists(Paths.get(outputPath));
29
30 String taggedPDF = outputPath + inputPDF +"-tagged-pdf.pdf";
31 String taggingReport = outputPath + inputPDF +"-tagging-report.xlsx";
32
33 // Initial setup, create credentials instance.
34 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
35 .fromFile("pdfservices-api-credentials.json")
36 .build();
37
38 // Create an ExecutionContext using credentials.
39 ExecutionContext executionContext = ExecutionContext.create(credentials);
40
41 AutotagPDFOperation autotagPDFOperation = AutotagPDFOperation.createNew();
42
43 // Build AutotagPDFOptions options and set them into the operation
44 AutotagPDFOptions autotagPDFOptions = AutotagPDFOptions.autotagPDFOptionsBuilder()
45 .shiftHeadings()
46 .generateReport()
47 .build();
48
49 autotagPDFOperation.setOptions(autotagPDFOptions);
50
51 // Execute the operation
52 AutotagPDFOutput result = autotagPDFOperation.execute(executionContext);
53
54 // Save the tagged PDF output at the specified location
55 autotagPDFOutput.getTaggedPDF().saveAs(taggedPDF);
56
57 // Save the tagging report output at the specified location
58 autotagPDFOutput.getReport().saveAs(taggingReport);
59
60 LOGGER.info("Successfully tagged information in PDF.");
61
62 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException e) {
63 LOGGER.error("Exception encountered while executing operation", e);
64 }
65 }
66}

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.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2023 Adobe. All rights reserved.