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

The following C# code example assembles a PDF document with unique page identifiers (bates numbering). Notice that the name of the DDX document is shell_Bates.xml. The PDF document that is returned from the Assembler service is saved as a PDF file named AssemblerResultBatesPDF.pdf. (See Assembling Documents Using Bates Numbering.)

???/** 
    * 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. 
    *  
    * 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"> 
    *   <Header> 
    *        <Center> 
    *            <StyledText> 
    *                <p font-size="20pt"><BatesNumber/></p> 
    *            </StyledText> 
    *        </Center> 
    *    </Header> 
    *      <PDF source="map.pdf" /> 
    *     <PDF source="directions.pdf" /> 
    *     </PDF> 
    * </DDX> 
    *      
    * For information, see "Invoking LiveCycle using MTOM" in Programming with LiveCycle   
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using AssembleBatesNumber.ServiceReference1; 
 
namespace AssembleBatesNumber 
{ 
       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 BLOBs 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_Bates.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; 
                   AssembleBatesNumber.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 that contains Bates numbering was assembled."); 
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
} 
 

// Ethnio survey code removed