Quick Start (SwaRef mode): Dynamically creating a DDX document using the web service API

The following Java code example dynamically 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 byte array. This method accepts an org.w3c.dom.Document object as an input parameter and returns a byte array

    The byte array returned from convertDDX is used to create a temporary XML file. The XML file is used to populate a FileDataSource object. This object is used to create a DataHandler object, which is used to populate the BLOB object’s SwaRef field. (See Dynamically Creating DDX Documents.)

        /** 
         * Ensure that you create Java proxy files that consume 
         * the Assembler services  WSDL. You can use JAX-WS to create 
         * the proxy Java files.   
         *    
         * For information, see "Invoking LiveCycle using SwaRef" 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 java.io.*; 
        import java.util.*; 
        import javax.activation.DataHandler; 
        import javax.activation.FileDataSource; 
        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 javax.xml.ws.BindingProvider; 
        import org.apache.xml.xml_soap.*; 
        import org.w3c.dom.Element; 
     
     
    import com.adobe.idp.services.*; 
     
        public class AssemblePDFWithDynamicDDXSWAref { 
             
            public static void main(String[] args){ 
                 
                try{ 
                    //Create an AssemblerServiceService object 
                    AssemblerServiceService assembler = new AssemblerServiceService(); 
                    AssemblerService assembClient = assembler.getAssemblerService(); 
                     
                    //Set connection values required to invoke LiveCycle 
                    String url = "http://hiro-xp:8080/soap/services/AssemblerService?blob=swaRef"; 
                    String username = "administrator"; 
                    String password = "password"; 
                    ((BindingProvider) assembClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
                    ((BindingProvider) assembClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
                    ((BindingProvider) assembClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
     
                    //Create the DDX required to disassemble a PDF document         
                    org.w3c.dom.Document ddx = createDDX();  
                    byte [] ddxByte = convertDDX(ddx); 
                     
                    //Populate the InputStream with the byte array 
                    InputStream inputStream = new ByteArrayInputStream(ddxByte);  
                     
                    //write the DDX out to a temp file 
                    File file = new File("C:\\temp.xml"); 
                    OutputStream out = new FileOutputStream(file); 
                             
                    //Iterate through the buffer 
                    int len; 
                    while ((len = inputStream.read(ddxByte)) > 0) 
                        out.write(ddxByte, 0, len); 
                    out.close(); 
                    inputStream.close(); 
                     
                    //Reference the temp DDX file and create a DataHandler object 
                    //to assign to the BLOB's object's setSwaRef field 
                    String path = "C:\\temp.xml"; 
                    FileDataSource ds = new FileDataSource(path); 
                    DataHandler handler1 = new DataHandler(ds); 
                    BLOB ddxDoc = new BLOB(); 
                    ddxDoc.setSwaRef(handler1);             
                     
                    //Reference the PDF document to disassemble  
                    String path2 = "C:\\AssemblerResultPDF.pdf"; 
                    FileDataSource ds2 = new FileDataSource(path2);  
                    DataHandler handler2 = new DataHandler(ds2); 
                    BLOB inDoc = new BLOB(); 
                    inDoc.setSwaRef(handler2); 
     
                    //Create the map containing input files 
                    MyMapOfXsdStringToXsdAnyType inputMap = new MyMapOfXsdStringToXsdAnyType(); 
                    List<MyMapOfXsdStringToXsdAnyTypeItem> inputMapItemList = inputMap.getItem(); 
                    MyMapOfXsdStringToXsdAnyTypeItem inputDocItem = new MyMapOfXsdStringToXsdAnyTypeItem(); 
                    inputDocItem.setKey("AssemblerResultPDF.pdf"); 
                    inputDocItem.setValue(inDoc); 
                    inputMapItemList.add(inputDocItem); 
     
                    //Create an AssemblerOptionsSpec object 
                    AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
                    assemblerSpec.setFailOnError(false); 
     
                    //Send the request to the Assembler Service 
                    AssemblerResult result = assembClient.invoke(ddxDoc, inputMap, assemblerSpec); 
     
                    //Extract the newly created PDF documents from the returned map 
                    List<MapItem> mapResult = result.getDocuments().getItem(); 
                    Iterator<MapItem> rit = mapResult.iterator(); 
                    int i = 0; 
                    while(rit.hasNext()) 
                    { 
                        MapItem resultItem = rit.next(); 
                        //Determine the data type of the map item element 
                        if (resultItem.getValue() instanceof BLOB) 
                        { 
                            //Save the disassembled PDF document as  
                            //a PDF file 
             File f = new File("C:\\ResultPDF"+i+".pdf"); 
                            BLOB outBlob = (BLOB)resultItem.getValue(); 
                            DataHandler handler3 = outBlob.getSwaRef(); 
     
                            //Get an InputStream from DataHandler and 
                            //write to a file 
                            InputStream inputStream2 = handler3.getInputStream(); 
                            OutputStream out2 = new FileOutputStream(f); 
     
                            //Iterate through the buffer 
                            byte buf[] = new byte[1024]; 
                            int len2; 
                            while ((len2 = inputStream2.read(buf)) > 0) 
                                out2.write(buf, 0, len2); 
                            out2.close(); 
                            inputStream2.close(); 
                       } 
                        i++; 
                    } 
                     
                    if (i > 0) 
                        System.out.println("The PDF document was disassembled into "+i+" PDF documents."); 
                    else 
                        System.out.println("The PDF document was not disassembled."); 
                         
                } 
                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 byte[] 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()); 
              } 
             
            //return the byte array 
              return mybytes;  
             } 
             
        }