# 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 5: Using a remote LiveCycle Service to create a PDF document Explain what LiveCycle ES is. Here are the slides to present: 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 Converting a PDF document to XDP. (Explain what XDP is). - Objective: Learn how to invoke an Adobe LiveCycle ES service using the Java SDK. - Expected duration: Approximately 15 minutes Step 1: Make sure you open up the CreatePDF class in the project called JavaOne2009. Navigate in Eclipse to `<lab_root>/PutUnderWorkspace/JavaOne2009` and open the file `CreatePDF.java` Step 2: The method signature we are going to call is as follows: createPDF converts different file formats into PDF documents. The supported file formats are Microsoft (MS) Word, MS Powerpoint, MS Excel, MS Project, MS Visio, MS Publisher, and Autocad. In addition to these, any third party generic application type for which you have a PDF generating application can be plugged into your application using this method. NOTE: For JavaOne2009 we will use PostScript to ease the server side processing. Some of the parameters are optional, which means that null values may be provided if they are not needed. Security permissions, PDF Output Settings and metadata information can optionally be applied to the resultant PDF document. Parameters inDoc — This mandatory parameter contains an instance of the com.adobe.idp.Document whose content is to be converted. For this object, it is recommended that you either use the appropriate com.adobe.idp.Document constructor if you have access to the file, or that you use the com.adobe.idp.Document.setAttribute() method if you only have access to the stream. inputFileName — This optional parameter contains the file name, with the appropriate extension, of the document to be converted. fileTypeSettings — Settings to be applied to the generated PDF document. You can create custom file type settings through the administration user interface. The available option is "Standard". pdfSettings — This optional parameter contains the PDF output settings to be applied. The following options are available: * "High Quality Print" * "Oversized Pages" * "PDFA1b 2005 CMYK" * "PDFA1b 2005 RGB" * "PDFX1a 2001" * "PDFX3 2002" * "Press Quality" * "Smallest File Size" * "Standard" You can also create custom settings via the Administration user interface. securitySettings — This optional parameter is used for applying security settings. The current available option is "No Security". You can also create custom settings via the Administration user interface. inSettingsDoc — This optional parameter contains the file containing settings to be applied while generating the PDF document (for example, optimization for web view) as well as the settings that are applied once the PDF document is created (for example, initial view and security). inXMPDoc — This optional parameter contains the file containing metadata information to be applied to the generated PDF document. Only UTF-8 encoded XMP metadata is supported. This service will returns the created PDF document and the log document, if any. Step 3: Make sure your starter code look something like this:

    package org.duanesworldtv.livecycle.samples;
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Properties;
    import com.adobe.livecycle.generatepdf.client.CreatePDFResult;
    import com.adobe.idp.Document;
    import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
    import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties;
    import com.adobe.livecycle.distiller.client.DistillerServiceClient;

    public class CreatePDF {
          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, "kvarsen");
        	    	ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password");

                // get pdf filename
        	       // TODO: Ensure the path is correct for you machine.
                String inFileName = "/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test.ps";

                // TODO: Create a ServiceClientFactory instance

                //TODO: Create a new instance of the DistillerServiceClient

                // inPdfName is the input PostScript file. Use absolute path to avoid errors.
                FileInputStream fileInputStream = new FileInputStream(inFileName);
                Document inDoc = new Document(fileInputStream);
                System.out.println("\nGrabbing input document " + inFileName);

                //TODO: Set run-time options for adobePDFSettings and securitySetting.

                //Convert a PS  file into a PDF file
                CreatePDFResult result = new CreatePDFResult();

                System.out.println("\nTrying to createPDF...");
                result = disClient.createPDF(
                            inDoc,
                            inFileName,
                            adobePDFSettings,
                            securitySettings,
                            null,
                            null
                      );

          //Get the newly created document
          Document createdDocument = result.getCreatedDocument();

          //Save the PDF file
          createdDocument.copyToFile(new File("converted_by_LC_" + System.currentTimeMillis() + ".pdf"));
          System.out.println("Conversion done!");
          } catch (Exception e) {
          e.printStackTrace();
          }
       }
    }
Note there are a few places in this file flagged with "TODO's". Some of these are simply changing the paths to the argument document and where to save the document. Please chance them to make them specific to your machine. Eclipse hint: To see all the "TODO's'" on any project in eclipse, select "Window" -> "Show View" -> "Other" -> "General" and select "Tasks". SPECIAL NOTE: in eclipse, if you are using WIndows, you need to either use the http URL style slashes ( example -c:/eclipse/workspace/JavaOne2009-docs/DuanesWorld.pdf /) in your path name or escape the path name by doubling up on back slashes like this: C:\\Development\\eclipse\\workspace\\JavaOne2009-docs\\test.ps Step 3: Red X's may appear after this but don't worry. We need to add code to create a PDF file. The first thing you should do is to check the input path to ensure it points as a valid postscript file. Step 4: Add code to create a ServiceClientFactory instance as shown below.

     // TODO: Create a ServiceClientFactory instance

    ServiceClientFactory factory = ServiceClientFactory.createInstance(ConnectionProps);
Step 5: Now you need to create a new instance of the DistillerServiceClient. Click the green arrow (or "Run" from the "Run" menu) to run it.

     //TODO: Create a new instance of the DistillerServiceClient

    DistillerServiceClient disClient = new DistillerServiceClient(factory );
Step 6: Now you need to specify the pdfSetting and SecuritySetting for the input document as per the method signature above. pdfSettings is an optional parameter contains the PDF output settings to be applied. We will use "Standard" and for security use "No Security" as shown below.

     //TODO: Set run-time options for adobePDFSettings and securitySetting.

     String adobePDFSettings = "Standard";
     String securitySettings = "No Security";
Step 7: You should now be ready to run your project. The code should look like the code below.

    package org.duanesworldtv.livecycle.samples;

      import java.io.File;

        import java.io.FileInputStream;

        import java.util.Properties;

        import com.adobe.livecycle.generatepdf.client.CreatePDFResult;

        import com.adobe.idp.Document;

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

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

        import com.adobe.livecycle.distiller.client.DistillerServiceClient;

        public class CreatePDF {

        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, "kvarsen");

        ConnectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password");

        // get pdf filename

        // TODO: Ensure the path is correct for you machine.

        String inFileName = "/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/test.ps";

        // TODO: Create a ServiceClientFactory instance

        ServiceClientFactory factory = ServiceClientFactory.createInstance(ConnectionProps);

        //TODO: Create a new instance of the DistillerServiceClient

        DistillerServiceClient disClient = new DistillerServiceClient(factory );

        // inPdfName is the input PostScript file. Use absolute path to avoid errors.

        FileInputStream fileInputStream = new FileInputStream(inFileName);

        Document inDoc = new Document(fileInputStream);

        System.out.println("\nGrabbing input document " + inFileName);

        //TODO: Set run-time options for adobePDFSettings and securitySetting.

        String adobePDFSettings = "Standard";

        String securitySettings = "No Security";

        //Convert a PS  file into a PDF file

        CreatePDFResult result = new CreatePDFResult();

        System.out.println("\nTrying to createPDF...");

        result = disClient.createPDF(

        inDoc,

        inFileName,

        adobePDFSettings,

        securitySettings,

        null,

        null

        );

        //Get the newly created document

        Document createdDocument = result.getCreatedDocument();

        //Save the PDF file

        createdDocument.copyToFile(new File("converted_by_LC_" + System.currentTimeMillis() + ".pdf"));

        System.out.println("Conversion done!");

        } catch (Exception e) {

        e.printStackTrace();

        }

        }

        }
Step 8: Navigate to the root directory and open up the newly created PDF file. Back to top Next exercise