# Lab-6808: Working with PDF and Java

Expected Duration: 120 minutes
Contacts: Duane Nickull

Introduction | Exercise 0 | Exercise 1 | Exercise 2 | Exercise 3 | Exercise 4 | Exercise 5 | Exercise 6 | Exercise 7 | Exercise 8 | Summary
### Exercise 6: Adding a visible signature field to a PDF document using the signature service client in LC ES. Teachers note: Digital Signatures and how they work For this exercise, we are now going to make a call to a server. The server instance is at HTTP://demo.ensemble.com:8080 The username and password are:kvarsen/password Adding a Signature Field to a PDF document. Here is the method signature - Objective: Learn how to invoke an Adobe LiveCycle ES service using the Java SDK to add a visible signature field to a PDF document. - Expected duration: Approximately 15 minutes Step 1: Make sure you open up the AddSignatureField class in the project called PDFConvert. Navitage in Eclipse to `<lab_root>/PutUnderWorkspace/JavaOne2009` and open the file `AddSignatureField.java` Step 2: Make sure your code looks similar to the following. There should 5 TODO's, 2 of which are merely paths.

    package org.duanesworldtv.livecycle.samples;

          import java.util.*;

        import java.io.File;

        import java.io.FileInputStream;

        import com.adobe.livecycle.signatures.client.*;

        import com.adobe.livecycle.signatures.client.types.*;

        import com.adobe.idp.Document;

        import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;

        import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties;

        public class AddSignatureField {

        public static void main(String[] args) {

        try

        {

        //Set connection properties required to invoke LiveCycle ES

        Properties ConnectionProps = new Properties();

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_SOAP_ENDPOINT, "http://demo.ensemble.com:8080");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL,ServiceClientFactoryProperties.DSC_SOAP_PROTOCOL);

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, "JBoss");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "p@ssw0rd");

        //Create a ServiceClientFactory instance

        ServiceClientFactory myFactory = ServiceClientFactory.createInstance(ConnectionProps);

        //Create a SignatureServiceClient object

        SignatureServiceClient signClient = new SignatureServiceClient(myFactory);

        //Specify a PDF document to which a signature field is added

        FileInputStream fileInputStream = new FileInputStream("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test.pdf");

        Document inDoc = new Document (fileInputStream);

        //TODO: Specify the name of the signature field

        //TODO: Create a  PositionRectangle object that specifies

        //the signature fields location

        //TODO: Specify the page number that will contain the signature field

        //Add a signature field to the PDF document

        Document sigFieldPDF = signClient.addSignatureField(

        inDoc,

        fieldName,

        pageNum,

        post,

        null,

        null);

        //Save the PDF document that contains the signature field

        File outFile = new File("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test-signed.pdf");

        System.out.println("Signature added, file saved!");

        sigFieldPDF.copyToFile(outFile);

        } catch (Exception ee) {

        ee.printStackTrace();

        }

        }

        }
Step 3: We need to add code to complete the parameters required to write the code where the "TODO" markers are. Study the method signature to understand what each required parameter is. Parameters inPDFDoc — A com.adobe.idp.Document object that represents the PDF document to which the signature field is added. This is a required parameter and cannot be null. signatureFieldName — The name of the signature field. This is a required parameter and cannot be null. pageNumber — The page number on which the signature field is added. Valid values are 1 to the number of pages contained within the document. This is a required parameter and cannot be null. positionRectangle — An PositionRectangle object that specifies the position for the signature field. This is a required parameter and cannot be null. If the specified rectangle does not lie at least partially on the crop box of the specified page, an InvalidArgumentException is thrown. Also, neither the height or width value of the specified rectangle can be 0 or negative. Lower left X or lower left Y coordinates can be 0 or greater but not negative, and are relative to the crop box of the page. fieldMDPOptionsSpec — A FieldMDPOptionSpec object that specifies the PDF document fields that are locked after the signature field is signed. This is an optional parameter and can be null. seedValueOptionsSpec — An SeedValueOptions object that specifies the various seed values for the field. This is an optional parameter and can be null. TODO: The first line of code you have to add will be the name of the signature filed itself. This can be whatever you want. Signature field in PDF documents are named uniquely so you can further manipulate them programmatically to do things like validate signatures, get signature values and more.

     //TODO: Specify the name of the signature field

    String fieldName = "SignatureField1";
Step 4: You now need tocCreate a "PositionRectangle" object that specifies the signature fields location. This is done via 4 integers which correspond to the (int lowerLeftX, int lowerLeftY, int width, int height). These represents the position of a signature field located within a PDF document. A signature field's rectangle defines the location of the signature field on the PDF document page in default user space units. An object of this type can be programmatically added to a PDF document.

     //TODO: Create a  PositionRectangle object that specifies

     //the signature fields location

    PositionRectangle post = new  PositionRectangle(193,47,133,12);
Step 5: The last thing you have to do is to specify the page number. This may be confusing but values start at 1 (not zero) and go up. This is a mandatory requirement and will throw an error if left null. HINT: The document we are using is only one page long.

    //TODO: Specify the page number that will contain the signature field

     java.lang.Integer pageNum = new java.lang.Integer(1);
Step 6: Your code should now be ready to compile and run. It should look like the code below:

    package org.duanesworldtv.livecycle.samples;
      /*

        * This Java Quick Start uses the following JAR files

        * 1. adobe-signatures-client.jar

        * 2. adobe-livecycle-client.jar

        * 3. adobe-usermanager-client.jar

        * 4. adobe-utilities.jar

        * 5. jbossall-client.jar (use a different JAR file if LiveCycle ES is not deployed

        * on JBoss)

        *

        * These JAR files are located in the following path:

        * <install directory>/Adobe/LiveCycle9.0/LiveCycle_ES_SDK/client-libs/common

        *

        * For complete details about the location of these JAR files,

        * see "Including LiveCycle ES library files" in Programming

        * with LiveCycle ES

        */

        import java.util.*;

        import java.io.File;

        import java.io.FileInputStream;

        import com.adobe.livecycle.signatures.client.*;

        import com.adobe.livecycle.signatures.client.types.*;

        import com.adobe.idp.Document;

        import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;

        import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties;

        public class AddSignatureField {

        public static void main(String[] args) {

        try

        {

        //Set connection properties required to invoke LiveCycle ES

        Properties ConnectionProps = new Properties();

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_SOAP_ENDPOINT, "http://demo.ensemble.com:8080");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL,ServiceClientFactoryProperties.DSC_SOAP_PROTOCOL);

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, "JBoss");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "p@ssw0rd");

        //Create a ServiceClientFactory instance

        ServiceClientFactory myFactory = ServiceClientFactory.createInstance(ConnectionProps);

        //Create a SignatureServiceClient object

        SignatureServiceClient signClient = new SignatureServiceClient(myFactory);

        //Specify a PDF document to which a signature field is added

        FileInputStream fileInputStream = new FileInputStream("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test.pdf");

        Document inDoc = new Document (fileInputStream);

        //Specify the name of the signature field

        String fieldName = "SignatureField1";

        //Create a  PositionRectangle object that specifies

        //the signature fields location (int lowerLeftX, int lowerLeftY, int width, int height)

        PositionRectangle post = new  PositionRectangle(193,47,133,35);

        //Specify the page number that will contain the signature field

        java.lang.Integer pageNum = new java.lang.Integer(1);

        //Add a signature field to the PDF document

        Document sigFieldPDF = signClient.addSignatureField(

        inDoc,

        fieldName,

        pageNum,

        post,

        null,

        null);

        //Save the PDF document that contains the signature field

        File outFile = new File("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test-signed.pdf");

        System.out.println("Signature added, file saved!");

        sigFieldPDF.copyToFile(outFile);

        } catch (Exception ee) {

        ee.printStackTrace();

        }

        }

        }
Now run the code. Note that this lab will take about 15 seconds to completely run. Probably longer if we are all logging in at the same time. Navigate to the path you specified for the output file and see your document. You should see a PDF document with a signature as shown below! Back to top Next exercise