# 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 8: Converting PDF to XDP by remotely invoking LiveCycle ES (SOAP) Xml Data Packages (XDP) are documents that are XML-based PDF documents. By turning the document "Inside out", developers get better access to the document using deterministic logic statements. XDP is used as the core of Adobe's enterprise service bus (LiveCycle ES). In this exercise, we are going to convert a PDF document to an XDP document. 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 to an XDP. Here is the method signature - Objective: Learn how to invoke an Adobe LiveCycle ES service using the Java SDK to convert a PDF document into a JPEG. - Expected duration: Approximately 15 minutes Step 1: Make sure you open up the ConvertPDFToXDP class in the project called JavaOne2009t. Navitage in Eclipse to `<lab_root>/PutUnderWorkspace/JavaOne2009` and open the file `ConvertPDFToXDP.java` Step 2: Make sure your code looks similar to the following. There should 2 TODO's, 1 of which is merely a path.

    package org.duanesworldtv.livecycle.samples;

      /*

        * This Java Quick Start uses the following JAR files

        * 1. adobe-pdfutility-client.jar

        * 2. adobe-livecycle-client.jar

        * 3. adobe-usermanager-client.jar

        * 4. adobe-utilities.jar

        * 5. jbossall-client.jar

        *

        * 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 com.adobe.livecycle.pdfutility.client.*;

        import java.io.*;

        import com.adobe.idp.Document;

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

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

        public class ConvertPDFToXDP

        {

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

        // Create a ServiceClientFactory object

        ServiceClientFactory myFactory = ServiceClientFactory.createInstance(ConnectionProps);

        // Create a PDF Utility client

        PDFUtilityServiceClient pdfUt = new PDFUtilityServiceClient(myFactory);

        // Specify a PDF document to convert to an XDP file

        //TODO: make sure this path is right for your machine

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

        System.out.println("Successfully grabbed " + fileInputStream);

        Document inDoc = new Document(fileInputStream);

        // Convert the PDF document to an XDP file

        System.out.println("Trying to convert....");

        //TODO: Try to convert the PDF into an XDP and save it

        }

        catch (Exception e)

        {

        System.out.println("Error occurred: " + e.getMessage());

        }

        }

        }
Step 3: We need to add code around line 55 (the second TODO) to invoke the service and save out our result to the local file. Instructions: - Make sure you use a try-catch construct. - examine the API docs to understand the exception (throws PDFUtilityException) - the solution is as follows:

     //TODO:

    			try
    			{
    				Document myXDP = pdfUt.convertPDFtoXDP(inDoc);
    				//Save the returned Document object as an XDP file
    				File xdpFile = new File("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/DuanesWorld.xdp");
    				myXDP.copyToFile(xdpFile);
    				System.out.println("Saved " + xdpFile + " to file...");
    			} catch (PDFUtilityException pdfue){
    				System.out.println("PDFUtility Exception " + pdfue);
    			}
Step 5: 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-pdfutility-client.jar

        * 2. adobe-livecycle-client.jar

        * 3. adobe-usermanager-client.jar

        * 4. adobe-utilities.jar

        * 5. jbossall-client.jar

        *

        * 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 com.adobe.livecycle.pdfutility.client.*;

        import java.io.*;

        import com.adobe.idp.Document;

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

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

        public class ConvertPDFToXDP

        {

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

        // Create a ServiceClientFactory object

        ServiceClientFactory myFactory = ServiceClientFactory.createInstance(ConnectionProps);

        // Create a PDF Utility client

        PDFUtilityServiceClient pdfUt = new PDFUtilityServiceClient(myFactory);

        // Specify a PDF document to convert to an XDP file

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

        System.out.println("Successfully grabbed " + fileInputStream);

        Document inDoc = new Document(fileInputStream);

        // Convert the PDF document to an XDP file

        System.out.println("Trying to convert....");

        //TODO: Try to convert the PDF into an XDP and save it

        try

        {

        Document myXDP = pdfUt.convertPDFtoXDP(inDoc);

        //Save the returned Document object as an XDP file

        File xdpFile = new File("/Users/duane/Desktop/eclipse/workspace/JavaOne2009-docs/DuanesWorld.xdp");

        myXDP.copyToFile(xdpFile);

        System.out.println("Saved " + xdpFile + " to file...");

        } catch (PDFUtilityException pdfue){

        System.out.println("PDFUtility Exception " + pdfue);

        }

        }

        catch (Exception e)

        {

        System.out.println("Error occurred: " + e.getMessage());

        }

        }

        }
Now run the code. Note that this lab will take about 15 seconds to completely run. The console will oputput the location of your saved file as shown below: Navigate to the path you specified for the output file and see your document. Back to top Summary