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

The following C# 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 a .NET project that uses  
    * MS Visual Studio 2008 and version 3.5 of the .NET 
    * framework. This is required to invoke a  
    * LiveCycle service using MTOM. 
    *      
    * For information, see "Invoking LiveCycle using MTOM" 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> 
    *  
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using AssembleBookmarks.ServiceReference1; 
 
namespace AssembleBookmarks 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create a RightsManagementServiceClient object 
                   AssemblerServiceClient assembleClient = new AssemblerServiceClient(); 
                   assembleClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/AssemblerService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)assembleClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   assembleClient.ClientCredentials.UserName.UserName = "administrator"; 
                   assembleClient.ClientCredentials.UserName.Password = "password"; 
                   b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
                   b.MaxReceivedMessageSize = 2000000; 
                   b.MaxBufferSize = 2000000; 
                   b.ReaderQuotas.MaxArrayLength = 2000000; 
     
                   // Create BLOBs that represents the input DDX file and input documents 
                   BLOB ddxDoc = new BLOB(); 
                   BLOB pdfDoc = new BLOB(); 
                   BLOB bookMarkDoc = new BLOB(); 
 
                   // Get the input DDX document, input PDF source file 
                   //and the Bookmark XML document 
                   string ddxFileName = "C:\\bookmarkDDX.xml"; 
                   FileStream ddxFs = new FileStream(ddxFileName, FileMode.Open); 
                   string pdfFile = "C:\\Loan.pdf"; 
                   FileStream pdfFileFs = new FileStream(pdfFile, FileMode.Open); 
                   string bookmarkInfo = "C:\\bookmarks.xml"; 
                   FileStream bookmarkInfoFs = new FileStream(bookmarkInfo, FileMode.Open); 
 
                   // Get the lengths of the file streams and create byte arrays 
                   int ddxLen = (int)ddxFs.Length; 
                   byte[] ddxByteArray = new byte[ddxLen]; 
                   int pdfLen = (int)pdfFileFs.Length; 
                   byte[] pdfByteArray = new byte[pdfLen]; 
                   int bookMarkLen = (int)bookmarkInfoFs.Length; 
                   byte[] bookMarkByteArray = new byte[bookMarkLen]; 
 
                   // Populate the byte arrays with the contents of the file streams 
                   ddxFs.Read(ddxByteArray, 0, ddxLen); 
                   pdfFileFs.Read(pdfByteArray, 0, pdfLen); 
                   bookmarkInfoFs.Read(bookMarkByteArray, 0, bookMarkLen); 
 
                   // Populate the BLOB objects 
                   ddxDoc.MTOM = ddxByteArray; 
                   pdfDoc.MTOM = pdfByteArray; 
                   bookMarkDoc.MTOM = bookMarkByteArray; 
 
                    // Create the map containing the PDF source documents 
                   MyMapOf_xsd_string_To_xsd_anyType inputMap = new MyMapOf_xsd_string_To_xsd_anyType(); 
 
                   MyMapOf_xsd_string_To_xsd_anyType_Item inLoan = new MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inLoan.key = "Loan.pdf"; 
                   inLoan.value = pdfDoc; 
 
                   MyMapOf_xsd_string_To_xsd_anyType_Item inBookmark = new MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inBookmark.key = "doc2"; 
                   inBookmark.value = bookMarkDoc; 
 
                   inputMap.Add(inLoan); 
                   inputMap.Add(inBookmark); 
 
                   // Create an AssemblerOptionsSpec object 
                   AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
                   assemblerSpec.failOnError = false; 
 
                   // Send the request to the Assembler Service 
                   AssemblerResult result = assembleClient.invoke(ddxDoc, inputMap, assemblerSpec); 
 
                   // Extract the newly created PDF document from the returned map 
                   BLOB outDoc = null; 
                   AssembleBookmarks.ServiceReference1.Map mapResult = result.documents; 
                   for (int i = 0; i < mapResult.Count; i++) 
                   { 
                       String myKey = (String)(mapResult[i].key); 
 
                       if (myKey == "FinalDoc.pdf") 
                       { 
                           outDoc = (BLOB)(mapResult[i].value); 
                       } 
                   } 
 
                   //Populate a byte array with BLOB data - using the MTOM field 
                   byte[] outByteArray = outDoc.MTOM; 
 
                   //Create a new file containing the returned PDF document 
                   string FILE_NAME = "C:\\AssemblerResultBookmarks.pdf"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                   BinaryWriter w = new BinaryWriter(fs2); 
                   w.Write(outByteArray); 
                   w.Close(); 
                   fs2.Close(); 
                   Console.WriteLine("The PDF file containing bookmarks was assembled."); 
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
} 

// Ethnio survey code removed