Quick Start (Java SWAref): Assembling a PDF document using the web service API

The following Java code example merges two PDF source documents named map.pdf and directions.pdf into a single PDF document. The name of the single PDF document is AssemblerResultPDF.pdf. The name of the DDX document is shell.xml. (See Programmatically Assembling PDF 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 validates the following DDX document: 
    *<?xml version="1.0" encoding="UTF-8"?> 
    * <DDX xmlns="http://ns.adobe.com/DDX/1.0/"> 
    *    <PDF result="out.pdf"> 
    *        <PDF source="map.pdf" /> 
    *        <PDF source="directions.pdf" /> 
    *    </PDF> 
    * </DDX> 
    */ 
 
import java.io.*; 
import java.util.*; 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.xml.ws.BindingProvider; 
import org.apache.xml.xml_soap.*; 
import com.adobe.idp.services.*; 
 
public class AssemblePDFSWAref { 
 
    public static void main(String[] args)  
    { 
        try  
        { 
            //Create an AssemblerService 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); 
 
            //Retrieve the input DDX and the PDF documents to assemble 
            String path = "C:\\shell.xml"; 
            FileDataSource ds = new FileDataSource(path);  
            DataHandler handler = new DataHandler(ds); 
            BLOB ddx = new BLOB(); 
            ddx.setSwaRef(handler); 
             
            String path2 = "C:\\map.pdf"; 
            FileDataSource ds2 = new FileDataSource(path2);  
            DataHandler handler2 = new DataHandler(ds2); 
            BLOB doc1 = new BLOB(); 
            doc1.setSwaRef(handler2); 
             
            String path3 = "C:\\directions.pdf"; 
            FileDataSource ds3 = new FileDataSource(path3);  
            DataHandler handler3 = new DataHandler(ds3); 
            BLOB doc2 = new BLOB(); 
            doc2.setSwaRef(handler3); 
                         
            // Create the map containing the PDF source documents 
            MyMapOfXsdStringToXsdAnyType inputMap = new MyMapOfXsdStringToXsdAnyType(); 
            List<MyMapOfXsdStringToXsdAnyTypeItem> inputMapItemList = inputMap.getItem(); 
            MyMapOfXsdStringToXsdAnyTypeItem doc1Item = new MyMapOfXsdStringToXsdAnyTypeItem(); 
            MyMapOfXsdStringToXsdAnyTypeItem doc2Item = new MyMapOfXsdStringToXsdAnyTypeItem();     
            doc1Item.setKey("map.pdf"); 
            doc1Item.setValue(doc1); 
            doc2Item.setKey("directions.pdf"); 
            doc2Item.setValue(doc2); 
            inputMapItemList.add(doc1Item); 
            inputMapItemList.add(doc2Item); 
             
            // Create an AssemblerOptionsSpec object 
            AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
            assemblerSpec.setFailOnError(false); 
 
            // Send the request to the Assembler Service 
            AssemblerResult result = assembClient.invoke(ddx,inputMap,assemblerSpec); 
 
            // Extract the newly created PDF document from the returned map 
            BLOB outDoc = null; 
            List<MapItem> mapResult = result.getDocuments().getItem(); 
            Iterator<MapItem> rit = mapResult.iterator(); 
            while(rit.hasNext()) 
            { 
                MapItem resultItem = rit.next(); 
                String myKey = (String)(resultItem.getKey()); 
 
                if (myKey.equals("out.pdf")) 
                { 
                    outDoc = (BLOB)(resultItem.getValue()); 
                } 
            } 
 
            //Create a PDF file that represents the assembled document 
            File f = new File("C:\\AssemblerResultPDF.pdf"); 
            BLOB outBlob = outDoc; 
            DataHandler handler4 = outBlob.getSwaRef(); 
             
            //Get an InputStream from DataHandler and 
            //write to the PDF file 
            InputStream inputStream = handler4.getInputStream(); 
            OutputStream out = new FileOutputStream(f); 
             
            //Iterate through the buffer 
            byte buf[] = new byte[1024]; 
            int len; 
            while ((len = inputStream.read(buf)) > 0) 
                out.write(buf, 0, len); 
            out.close(); 
            inputStream.close(); 
 
        } 
        catch(Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 
     
}