Quick Start (SwaRef): Assembling PDF documents with bookmarks using the web service API

The following Java code example assembles a PDF document that contains bookmarks. The name of the DDX document is bookmarkDDX.xml. The name of the bookmark XML document that describes the bookmarks to add to the PDF document is bookmarks.xml. The result PDF document is saved as a PDF file named AssemblerResultBookmarks.pdf. (See Assembling PDF Documents with Bookmarks.)

/** 
    * 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 uses the following DDX document: 
    * <?xml version="1.0" encoding="UTF-8"?> 
    * <DDX xmlns="http://ns.adobe.com/DDX/1.0/"> 
    *  <PDF result="FinalDoc.pdf"> 
    *     <PDF source="Loan.pdf"> 
    *        <Bookmarks source="doc2" /> 
    *     </PDF> 
    *  </PDF> 
    * </DDX> 
    *  
    * This quick start also uses the following bookmarks XML   
    * to assemble a PDF document containing bookmarks: 
    * <?xml version="1.0" encoding="UTF-8"?> 
    * <Bookmarks xmlns="http://ns.adobe.com/pdf/bookmarks" version="1.0"> 
    *  <Bookmark> 
    *     <Action> 
    *        <Launch NewWindow="true"> 
    *           <File Name="C:\Adobe\LoanDetails.pdf" /> 
    *        </Launch> 
    *     </Action> 
    *        <Title>Open the Loan document</Title> 
    *  </Bookmark> 
    * <Bookmark> 
    *     <Action> 
    *        <Launch> 
    *           <Win Name="C:\WINDOWS\notepad.exe" /> 
    *        </Launch> 
    *     </Action> 
    *    <Title>Launch NotePad</Title> 
    *  </Bookmark> 
    * </Bookmarks> 
    *  
    */ 
 
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 AssembleBookmarksSWAref { 
    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); 
             
               // Get the input DDX document, input PDF source file 
               //and the Bookmark DDX document 
               String path = "C:\\bookmarkDDX.xml"; 
            FileDataSource ds = new FileDataSource(path);  
            DataHandler handler = new DataHandler(ds); 
            BLOB ddxDoc = new BLOB(); 
            ddxDoc.setSwaRef(handler); 
             
               String path2 = "C:\\Loan.pdf"; 
            FileDataSource ds2 = new FileDataSource(path2);  
            DataHandler handler2 = new DataHandler(ds2); 
            BLOB pdfDoc = new BLOB(); 
            pdfDoc.setSwaRef(handler2); 
             
               String path3 = "C:\\bookmarks.xml"; 
            FileDataSource ds3 = new FileDataSource(path3);  
            DataHandler handler3 = new DataHandler(ds3); 
            BLOB bookMarkDoc = new BLOB(); 
            bookMarkDoc.setSwaRef(handler3); 
             
               //Create the map containing the input files 
            MyMapOfXsdStringToXsdAnyType inputMap = new MyMapOfXsdStringToXsdAnyType(); 
            List<MyMapOfXsdStringToXsdAnyTypeItem> inputMapItemList = inputMap.getItem(); 
            MyMapOfXsdStringToXsdAnyTypeItem loanItem = new MyMapOfXsdStringToXsdAnyTypeItem(); 
            MyMapOfXsdStringToXsdAnyTypeItem doc2Item = new MyMapOfXsdStringToXsdAnyTypeItem(); 
            loanItem.setKey("Loan.pdf"); 
            loanItem.setValue(pdfDoc); 
            doc2Item.setKey("doc2"); 
            doc2Item.setValue(bookMarkDoc); 
            inputMapItemList.add(loanItem); 
            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(ddxDoc, 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("FinalDoc.pdf")) 
                   { 
                       outDoc = (BLOB)(resultItem.getValue()); 
                   } 
               } 
 
            File f = new File("C:\\AssemblerResultBookmarks.pdf"); 
            BLOB outBlob = outDoc; 
            DataHandler handler4 = outBlob.getSwaRef(); 
             
            //Get an InputStream from DataHandler and 
            //write to a 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(); 
 
               System.out.println("The PDF file containing bookmarks was assembled.");             
        } 
        catch(Exception e) 
        { 
            e.printStackTrace(); 
        } 
     
    } 
 
} 

// Ethnio survey code removed