Quickstart for Adobe PDF Electronic Seal API (Java)
To get started using Adobe PDF Electronic Seal API, let's walk through a simple scenario - Applying an electronic seal on an invoice PDF document. 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-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 recommended and not a federated ID.
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."
6) After your credentials are created, they are automatically downloaded:
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.
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 contents:
Copied to your clipboard1<?xml version="1.0" encoding="UTF-8"?>23<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>78 <groupId>com.adobe.documentservices</groupId>9 <artifactId>pdfservices-sdk-electronicseal-guide</artifactId>10 <version>1</version>1112 <name>PDF Services Java SDK Samples</name>1314 <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.4.0</pdfservices.sdk.version>19 </properties>2021 <dependencies>2223 <dependency>24 <groupId>com.adobe.documentservices</groupId>25 <artifactId>pdfservices-sdk</artifactId>26 <version>${pdfservices.sdk.version}</version>27 </dependency>2829 <!-- 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>3738 <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>ElectronicSeal</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 an Invoice PDF document, sampleInvoice.pdf
(downloadable from here), and will use the sealing options with default appearance options to apply electronic seal over the PDF document by invoking Acrobat Services API and generate an electronically sealed PDF.
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 ElectronicSeal.java
.
Now you're ready to begin coding.
Step Three: Creating the application
1) We will begin by including the required dependencies:
Copied to your clipboard1import 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.pdfops.PDFElectronicSealOperation;8import com.adobe.pdfservices.operation.pdfops.options.electronicseal.FieldLocation;9import com.adobe.pdfservices.operation.pdfops.options.electronicseal.FieldOptions;10import com.adobe.pdfservices.operation.pdfops.options.electronicseal.CSCAuthContext;11import com.adobe.pdfservices.operation.pdfops.options.electronicseal.CertificateCredentials;12import com.adobe.pdfservices.operation.pdfops.options.electronicseal.SealOptions;13import com.adobe.pdfservices.operation.pdfops.options.electronicseal.SignatureFormat;14import com.adobe.pdfservices.operation.pdfops.options.electronicseal.AppearanceOptions;15import com.adobe.pdfservices.operation.pdfops.options.electronicseal.AppearanceItem;16import org.slf4j.Logger;17import org.slf4j.LoggerFactory;18import java.io.IOException;
2) Now let's define our main class:
Copied to your clipboard1public class ElectronicSeal {23 // Initialize the logger.4 private static final Logger LOGGER = LoggerFactory.getLogger(ElectronicSeal.class);56 public static void main(String[] args) {78 }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) Let's create credentials for pdf services and use them:
Copied to your clipboard1// Initial setup, create credentials instance.2Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()3 .withClientId("PDF_SERVICES_CLIENT_ID")4 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")5 .build();67// Create an ExecutionContext using credentials.8ExecutionContext executionContext = ExecutionContext.create(credentials);
5) Now, let's define our input fields:
Copied to your clipboard1//Get the input document to perform the sealing operation2FileRef sourceFile = FileRef.createFromLocalFile("./sampleInvoice.pdf");34//Get the background seal image for signature , if required.5FileRef sealImageFile = FileRef.createFromLocalFile("./sampleSealImage.png");
6) Now, we will define seal field options:
Copied to your clipboard1//Create AppearanceOptions and add the required signature display items to it2AppearanceOptions appearanceOptions = new AppearanceOptions();3appearanceOptions.addItem(AppearanceItem.NAME);4appearanceOptions.addItem(AppearanceItem.LABELS);5appearanceOptions.addItem(AppearanceItem.DATE);6appearanceOptions.addItem(AppearanceItem.SEAL_IMAGE);7appearanceOptions.addItem(AppearanceItem.DISTINGUISHED_NAME);89//Set the Seal Field Name to be created in input PDF document.10String sealFieldName = "Signature1";1112//Set the page number in input document for applying seal.13Integer sealPageNumber = 1;1415//Set if seal should be visible or invisible.16Boolean sealVisible = true;1718//Create FieldLocation instance and set the coordinates for applying signature19FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);2021//Create FieldOptions instance with required details.22FieldOptions fieldOptions = new FieldOptions.Builder(sealFieldName)23 .setFieldLocation(fieldLocation)24 .setPageNumber(sealPageNumber)25 .setVisible(sealVisible)26 .build();
7) Next, we create a CSC Certificate Credentials instance:
Copied to your clipboard1//Set the name of TSP Provider being used.2String providerName = "<PROVIDER_NAME>";34//Set the access token to be used to access TSP provider hosted APIs.5String accessToken = "<ACCESS_TOKEN>";67//Set the credential ID.8String credentialID = "<CREDENTIAL_ID>";910//Set the PIN generated while creating credentials.11String pin = "<PIN>";1213//Create CSCAuthContext instance using access token and token type.14CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");1516//Create CertificateCredentials instance with required certificate details.17CertificateCredentials certificateCredentials = CertificateCredentials.cscCredentialBuilder()18 .withProviderName(providerName)19 .withCredentialID(credentialID)20 .withPin(pin)21 .withCSCAuthContext(cscAuthContext)22 .build();23
8) Now, let's create the seal options with certificate credentials and field options:
Copied to your clipboard1//Create SealOptions instance with all the sealing parameters.2SealOptions sealOptions = new SealOptions.Builder(certificateCredentials, fieldOptions)3 .withAppearanceOptions(appearanceOptions).build();
9) Now, let's create the operation:
Copied to your clipboard1//Create the PDFElectronicSealOperation instance using the SealOptions instance2PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.createNew(sealOptions);34//Set the input source file for PDFElectronicSealOperation instance5pdfElectronicSealOperation.setInput(sourceFile);67//Set the optional input seal image for PDFElectronicSealOperation instance8pdfElectronicSealOperation.setSealImage(sealImageFile);
This code creates a seal operation using seal options, input source file and input seal image.
10) Let's execute this seal operation:
Copied to your clipboard1//Execute the operation2FileRef result = pdfElectronicSealOperation.execute(executionContext);34//Save the output at specified location5result.saveAs("output/sealedOutput.pdf");
Here's the complete application (src/main/java/ElectronicSeal.java
):
Copied to your clipboard1import 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.pdfops.PDFElectronicSealOperation;8import com.adobe.pdfservices.operation.pdfops.options.electronicseal.FieldLocation;9import com.adobe.pdfservices.operation.pdfops.options.electronicseal.FieldOptions;10import com.adobe.pdfservices.operation.pdfops.options.electronicseal.CSCAuthContext;11import com.adobe.pdfservices.operation.pdfops.options.electronicseal.CertificateCredentials;12import com.adobe.pdfservices.operation.pdfops.options.electronicseal.SealOptions;13import com.adobe.pdfservices.operation.pdfops.options.electronicseal.SignatureFormat;14import com.adobe.pdfservices.operation.pdfops.options.electronicseal.AppearanceOptions;15import com.adobe.pdfservices.operation.pdfops.options.electronicseal.AppearanceItem;16import org.slf4j.Logger;17import org.slf4j.LoggerFactory;18import java.io.IOException;1920/**21 * This sample ElectronicSeal illustrates how to apply electronic seal over the PDF document using custom appearance options.22 *23 * <p>24 * To know more about PDF Electronic Seal, please see the <a href="https://developer.adobe.com/document-services/docs/overview/pdf-electronic-seal-api/" target="_blank">documentation</a>.25 * <p>26 * Refer to README.md for instructions on how to run the samples.27 */28public class ElectronicSeal {2930 // Initialize the logger.31 private static final Logger LOGGER = LoggerFactory.getLogger(ElectronicSeal.class);3233 public static void main(String[] args) {34 try {35 // Initial setup, create credentials instance.36 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()37 .withClientId("PDF_SERVICES_CLIENT_ID")38 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")39 .build();4041 // Create an ExecutionContext using credentials.42 ExecutionContext executionContext = ExecutionContext.create(credentials);4344 //Get the input document to perform the sealing operation45 FileRef sourceFile = FileRef.createFromLocalFile("./sampleInvoice.pdf");4647 //Get the background seal image for signature , if required.48 FileRef sealImageFile = FileRef.createFromLocalFile("./sampleSealImage.png");4950 //Create AppearanceOptions and add the required signature display items to it51 AppearanceOptions appearanceOptions = new AppearanceOptions();52 appearanceOptions.addItem(AppearanceItem.NAME);53 appearanceOptions.addItem(AppearanceItem.LABELS);54 appearanceOptions.addItem(AppearanceItem.DATE);55 appearanceOptions.addItem(AppearanceItem.SEAL_IMAGE);56 appearanceOptions.addItem(AppearanceItem.DISTINGUISHED_NAME);5758 //Set the Seal Field Name to be created in input PDF document.59 String sealFieldName = "signature1";6061 //Set the page number in input document for applying seal.62 Integer sealPageNumber = 1;6364 //Set if seal should be visible or invisible.65 Boolean sealVisible = true;6667 //Create FieldLocation instance and set the coordinates for applying signature68 FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);6970 //Create FieldOptions instance with required details.71 FieldOptions fieldOptions = new FieldOptions.Builder(sealFieldName)72 .setFieldLocation(fieldLocation)73 .setPageNumber(sealPageNumber)74 .setVisible(sealVisible)75 .build();7677 //Set the name of TSP Provider being used.78 String providerName = "<PROVIDER_NAME>";7980 //Set the access token to be used to access TSP provider hosted APIs.81 String accessToken = "<ACCESS_TOKEN>";8283 //Set the credential ID.84 String credentialID = "<CREDENTIAL_ID>";8586 //Set the PIN generated while creating credentials.87 String pin = "<PIN>";8889 //Create CSCAuthContext instance using access token and token type.90 CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");9192 //Create CertificateCredentials instance with required certificate details.93 CertificateCredentials certificateCredentials = CertificateCredentials.cscCredentialBuilder()94 .withProviderName(providerName)95 .withCredentialID(credentialID)96 .withPin(pin)97 .withCSCAuthContext(cscAuthContext)98 .build();99100 //Create SealOptions instance with all the sealing parameters.101 SealOptions sealOptions = new SealOptions.Builder(certificateCredentials, fieldOptions)102 .withAppearanceOptions(appearanceOptions).build();103104 //Create the PDFElectronicSealOperation instance using the SealOptions instance105 PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.createNew(sealOptions);106107 //Set the input source file for PDFElectronicSealOperation instance108 pdfElectronicSealOperation.setInput(sourceFile);109110 //Set the optional input seal image for PDFElectronicSealOperation instance111 pdfElectronicSealOperation.setSealImage(sealImageFile);112113 //Execute the operation114 FileRef result = pdfElectronicSealOperation.execute(executionContext);115116 //Save the output at specified location117 result.saveAs("output/sealedOutput.pdf");118119120 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {121 LOGGER.error("Exception encountered while executing operation", ex);122 }123 }124}
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.