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

The following C# 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 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  
    *  
    * The following XML represents the DDX document used in this quick start: 
    * <?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> 
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using AssembleDocument.ServiceReference1; 
 
namespace AssembleDocument 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create an AssemblerServiceClient 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 BLOB objects that represents the input DDX file and PDF sources 
                   BLOB ddxDoc = new BLOB(); 
                   BLOB mapDoc = new BLOB(); 
                   BLOB directionsDoc = new BLOB(); 
 
                   // Get the input DDX document and input PDF sources 
                   string ddxFileName = "C:\\shell.xml"; 
                   FileStream ddxFs = new FileStream(ddxFileName, FileMode.Open); 
                   string pdfFileNameMap = "C:\\map.pdf"; 
                   FileStream mapFs = new FileStream(pdfFileNameMap, FileMode.Open); 
                   string pdfFileNameOptions = "C:\\directions.pdf"; 
                   FileStream directionsFs = new FileStream(pdfFileNameOptions, FileMode.Open); 
 
                   // Get the lengths of the file streams and create byte arrays 
                   int ddxLen = (int)ddxFs.Length; 
                   byte[] ddxByteArray = new byte[ddxLen]; 
                   int mapLen = (int)mapFs.Length; 
                   byte[] mapByteArray = new byte[mapLen]; 
                   int directionsLen = (int)directionsFs.Length; 
                   byte[] directionsByteArray = new byte[directionsLen]; 
 
                   // Populate the byte arrays with the contents of the file streams 
                   ddxFs.Read(ddxByteArray, 0, ddxLen); 
                   mapFs.Read(mapByteArray, 0, mapLen); 
                   directionsFs.Read(directionsByteArray, 0, directionsLen); 
 
                   // Populate the BLOB objects 
                   ddxDoc.MTOM = ddxByteArray; 
                   mapDoc.MTOM = mapByteArray; 
                   directionsDoc.MTOM = directionsByteArray; 
 
                   // 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 inMapDoc = new MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inMapDoc.key = "map.pdf"; 
                   inMapDoc.value = mapDoc;  
 
                   MyMapOf_xsd_string_To_xsd_anyType_Item inDirDoc = new MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inDirDoc.key = "directions.pdf"; 
                   inDirDoc.value = directionsDoc;  
     
                   inputMap.Add(inMapDoc); 
                   inputMap.Add(inDirDoc); 
 
                   // 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; 
                   AssembleDocument.ServiceReference1.Map mapResult = result.documents; 
                   for (int i = 0; i < mapResult.Count; i++) 
                   { 
                       String myKey = (String)(mapResult[i].key); 
 
                       if (myKey == "out.pdf") 
                       { 
                           outDoc = (BLOB)(mapResult[i].value); 
                       } 
                   } 
 
                   //Populate a byte array with the BLOB 
                   byte[] outByteArray = outDoc.MTOM; 
 
                   //Create a new file containing the returned PDF document 
                   string FILE_NAME = "C:\\AssemblerResultPDF.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 files were assembled into AssemblerResultPDF.pdf."); 
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
}