Edit in GitHubLog an issue

Quickstart for Adobe PDF Services API (Java)

To get started using Adobe PDF Services API, let's walk through a simple scenario - taking an input PDF document and exporting it to Microsoft Word. 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-services-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 the pdfservices-api-credentials.json 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-pdf-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, Bodea Brochure.pdf (downloadable from here) and convert it to a Microsoft Word document, Bodea Brochure.docx.

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 ExportPDFToWord.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.ExportPDFJob;
12import com.adobe.pdfservices.operation.pdfjobs.params.exportpdf.ExportPDFParams;
13import com.adobe.pdfservices.operation.pdfjobs.params.exportpdf.ExportPDFTargetFormat;
14import com.adobe.pdfservices.operation.pdfjobs.result.ExportPDFResult;
15import org.apache.commons.io.IOUtils;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import java.io.File;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.OutputStream;
23import java.nio.file.Files;
24import java.nio.file.Paths;

2) Now let's define our main class:

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

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 use them:

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());

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

Copied to your clipboard
1// Create parameters for the job
2ExportPDFParams exportPDFParams = ExportPDFParams.exportPDFParamsBuilder(ExportPDFTargetFormat.DOCX).build();
3
4// Creates a new job instance
5ExportPDFJob exportPDFJob = new ExportPDFJob(asset, exportPDFParams);

This set of code defines what we're doing (an Export operation), and sets parameter for the Export PDF job. In this example, the only parameter is the export format ,ie, DOCX.

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(exportPDFJob);
3PDFServicesResponse<ExportPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, ExportPDFResult.class);
4
5// Get content from the resulting asset(s)
6Asset resultAsset = pdfServicesResponse.getResult().getAsset();
7StreamAsset streamAsset = pdfServices.getContent(resultAsset);

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

Copied to your clipboard
1// Creates an output stream and copy stream asset's content to it
2OutputStream outputStream = Files.newOutputStream(new File("./Bodea Brochure.docx").toPath());
3IOUtils.copy(streamAsset.getInputStream(), outputStream);

This code runs the Export process and then stores the result Word document to the file system.

Example running in the command line

Here's the complete application (src/main/java/ExportPDFToWord.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.ExportPDFJob;
12import com.adobe.pdfservices.operation.pdfjobs.params.exportpdf.ExportPDFParams;
13import com.adobe.pdfservices.operation.pdfjobs.params.exportpdf.ExportPDFTargetFormat;
14import com.adobe.pdfservices.operation.pdfjobs.result.ExportPDFResult;
15import org.apache.commons.io.IOUtils;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import java.io.File;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.OutputStream;
23import java.nio.file.Files;
24import java.nio.file.Paths;
25
26public class ExportPDFToWord {
27
28 private static final Logger LOGGER = LoggerFactory.getLogger(ExportPDFToWord.class);
29
30 public static void main(String[] args) {
31
32 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/Bodea Brochure.pdf").toPath())) {
33 // Initial setup, create credentials instance
34 Credentials credentials = new ServicePrincipalCredentials(
35 System.getenv("PDF_SERVICES_CLIENT_ID"),
36 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
37
38 // Creates a PDF Services instance
39 PDFServices pdfServices = new PDFServices(credentials);
40
41 // Creates an asset(s) from source file(s) and upload
42 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
43
44 // Create parameters for the job
45 ExportPDFParams exportPDFParams = ExportPDFParams.exportPDFParamsBuilder(ExportPDFTargetFormat.DOCX)
46 .build();
47
48 // Creates a new job instance
49 ExportPDFJob exportPDFJob = new ExportPDFJob(asset, exportPDFParams);
50
51 // Submit the job and gets the job result
52 String location = pdfServices.submit(exportPDFJob);
53 PDFServicesResponse<ExportPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, ExportPDFResult.class);
54
55 // Get content from the resulting asset(s)
56 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
57 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
58
59 // Creates an output stream and copy stream asset's content to it
60 Files.createDirectories(Paths.get("output/"));
61 OutputStream outputStream = Files.newOutputStream(new File("output/Bodea Brochure.docx").toPath());
62 LOGGER.info("Saving asset at output/Bodea Brochure.docx");
63 IOUtils.copy(streamAsset.getInputStream(), outputStream);
64 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException e) {
65 LOGGER.error("Exception encountered while executing operation", e);
66 }
67 }
68}

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.