Quick Start (EJB mode): Dynamically creating a DDX document using the Java API

The following Java code example dynamally creates a DDX document that disassembles a PDF document. A new PDF document is created for each level 1 bookmark in the input PDF document. This code example contains two user-defined methods:

  • createDDX: Creates an org.w3c.dom.Document object that represents the DDX document that is sent to the Assembler service. This user-defined method returns the org.w3c.dom.Document object.

  • convertDDX: Converts an org.w3c.dom.Document object to a com.adobe.idp.Document object. This method accepts an org.w3c.dom.Document object as an input parameter and returns a com.adobe.idp.Document object.

    Both of these methods are invoked in this quick start. (See Dynamically Creating DDX Documents.)

    /* 
        * This Java Quick Start uses the EJB mode and contains the following JAR files 
        * in the class path: 
        * 1. adobe-assembler-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 the LiveCycle Server is not deployed 
        * on JBoss) 
        * 6. jacorb.jar (use a different JAR file if the LiveCycle Server is not deployed on JBoss) 
        * 7. jnp-client.jar (use a different JAR file if the LiveCycle Server is not deployed on JBoss) 
        *  
        *  The JBoss files must be kept in the jboss\client folder. You can copy the client folder to  
        *  your local development environment and then include the 3 JBoss JAR files in your class path 
        *   
        *  These JAR files are located in the following path: 
        * <install directory>/sdk/client-libs/common 
        *  
        * The adobe-utilities.jar file is located in the following path: 
        * <install directory>/sdk/client-libs/jboss 
        *  
        * The jbossall-client.jar file is located in the following path: 
        * <install directory>/jboss/client 
        *  
        * If you want to invoke a remote LiveCycle Server instance and there is a 
        * firewall between the client application and the server, then it is  
        * recommended that you use the SOAP mode. When using the SOAP mode,  
        * you have to include additional JAR files located in the following  
        * path 
        * <install directory>/sdk/client-libs/thirdparty 
        *  
        * For information about the SOAP  
        * mode and the additional JAR files that need to be included,  
        * see "Setting connection properties" in Programming  
        * with LiveCycle 
        *  
        * For complete details about the location of the LiveCycle JAR files,  
        * see "Including LiveCycle Java library files" in Programming  
        * with LiveCycle 
        *  
        * This quick start dynamically creates the following DDX document: 
        * <?xml version="1.0" encoding="UTF-8"?> 
        * <DDX xmlns="http://ns.adobe.com/DDX/1.0/"> 
        *  <PDFsFromBookmarks prefix="stmt"> 
        *    <PDF source="AssemblerResultPDF.pdf"/> 
        *</PDFsFromBookmarks> 
        * </DDX> 
        */ 
    import com.adobe.livecycle.assembler.client.*; 
     
    import java.util.*; 
    import java.io.ByteArrayOutputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
     
    import javax.xml.parsers.DocumentBuilder; 
    import javax.xml.parsers.DocumentBuilderFactory; 
    import javax.xml.transform.Transformer; 
    import javax.xml.transform.TransformerFactory; 
    import javax.xml.transform.dom.DOMSource; 
    import javax.xml.transform.stream.StreamResult; 
     
    import org.w3c.dom.Element; 
     
    import com.adobe.idp.Document; 
    import com.adobe.idp.dsc.clientsdk.ServiceClientFactory; 
    import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties; 
     
    public class AssemblePDFWithDynamicDDX 
    { 
        public static void main(String[] args) { 
            try{ 
                //Set connection properties required to invoke LiveCycle                                 
                Properties connectionProps = new Properties(); 
                connectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_EJB_ENDPOINT, "jnp://hiro-xp:1099"); 
                connectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL,ServiceClientFactoryProperties.DSC_EJB_PROTOCOL);           
                connectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, "JBoss"); 
                connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator"); 
                connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password"); 
                 
                //Create a ServiceClientFactory instance 
                ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps); 
                 
                //Create an AssemblerServiceClient object 
                AssemblerServiceClient assemblerClient = new AssemblerServiceClient(myFactory); 
                     
                //Dynamically create a DDX document 
                org.w3c.dom.Document myDDX= createDDX() ;  
     
                //Covert the DDX document to a com.adobe.idp.Document instance     
                com.adobe.idp.Document ddx = convertDDX(myDDX); 
                             
                //Create a Map object to store PDF source documents 
                Map inputs = new HashMap(); 
                FileInputStream mySourceMap = new FileInputStream("C:\\AssemblerResultPDF.pdf"); 
                 
                //Create a Document object based on the map.pdf source file 
                Document myPDFSource = new Document(mySourceMap);  
     
                //Place the entry into the Map object 
                inputs.put("AssemblerResultPDF.pdf",myPDFSource);  
                 
                //Create an AssemblerOptionsSpec object 
                AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
                assemblerSpec.setFailOnError(false);  
     
                //Submit the job to Assembler service and use the dynamically created DDX document 
                AssemblerResult jobResult = assemblerClient.invokeDDX(ddx,inputs,assemblerSpec);    
                java.util.Map allDocs = jobResult.getDocuments(); 
     
                //Retrieve the result PDF document from the Map object 
                Document outDoc = null; 
                int index = 1; 
     
                //Iterate through the map object to retrieve the result PDF documents 
                for (Iterator i = allDocs.entrySet().iterator(); i.hasNext();) { 
                    // Retrieve the Map object?s value 
                    Map.Entry e = (Map.Entry)i.next(); 
                    Object o = e.getValue(); 
     
                    //Cast the Object to a Document 
                    //and save to a file     
                    outDoc = (Document)o; 
                    File myOutFile = new File("C:\\ResultPDF"+index +".pdf"); 
                    outDoc.copyToFile(myOutFile); 
                    index++; 
                } 
                 
            }catch (Exception e) { 
                e.printStackTrace(); 
            } 
        } 
         
         //Creates a DDX document using an org.w3c.dom.Document object 
         private static org.w3c.dom.Document createDDX() 
         { 
            org.w3c.dom.Document document = null; 
             
            try 
            { 
                //Create DocumentBuilderFactory and DocumentBuilder objects 
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
                DocumentBuilder builder = factory.newDocumentBuilder(); 
     
                //Create a new Document object 
                document = builder.newDocument();  
     
                //Create the root element and append it to the XML DOM 
                Element root = (Element)document.createElement("DDX"); 
                root.setAttribute("xmlns","http://ns.adobe.com/DDX/1.0/"); 
                document.appendChild(root); 
                     
                //Create the PDFsFromBookmarks element 
                Element PDFsFromBookmarks = (Element)document.createElement("PDFsFromBookmarks"); 
                PDFsFromBookmarks.setAttribute("prefix","stmt"); 
                root.appendChild(PDFsFromBookmarks); 
                 
                //Create the PDF element 
                Element PDF = (Element)document.createElement("PDF"); 
                PDF.setAttribute("source","AssemblerResultPDF.pdf"); 
                PDFsFromBookmarks.appendChild(PDF); 
          } 
         catch (Exception e) { 
                  System.out.println("The following exception occurred: "+e.getMessage()); 
               } 
        return document; 
         } 
         
         
         //Converts an org.w3c.dom.Document object to a  
         //com.adobe.idp.Document object  
         private static Document convertDDX(org.w3c.dom.Document myDOM) 
         { 
            byte[] mybytes = null;  
              
            try 
            { 
            //Create a Java Transformer object 
            TransformerFactory transFact = TransformerFactory.newInstance(); 
            Transformer transForm = transFact.newTransformer();  
     
            //Create a Java ByteArrayOutputStream object 
            ByteArrayOutputStream myOutStream = new ByteArrayOutputStream();  
     
            //Create a Java Source object 
            javax.xml.transform.dom.DOMSource myInput = new DOMSource(myDOM); 
         
            //Create a Java Result object 
            javax.xml.transform.stream.StreamResult myOutput = new StreamResult(myOutStream);      
     
            //Populate the Java ByteArrayOutputStream object 
            transForm.transform(myInput,myOutput); 
     
            // Get the size of the ByteArrayOutputStream buffer 
             int myByteSize = myOutStream.size(); 
         
            //Allocate myByteSize to the byte array 
             mybytes = new byte[myByteSize]; 
                 
            //Copy the content to the byte array 
             mybytes = myOutStream.toByteArray(); 
            } 
            catch (Exception e) { 
              System.out.println("The following exception occurred: "+e.getMessage()); 
          } 
         
         //Create a com.adobe.idp.Document object and copy the  
         //contents of the byte array 
         Document myDocument = new Document(mybytes); 
         return myDocument;  
         } 
         
    }

// Ethnio survey code removed