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

The following C# code example disassembles a PDF document named AssemblerResultPDF.pdf. Notice that the name of the DDX document is shell_disassemble.xml. Each disassembled PDF document is named ResultPDF[Number].pdf. That is, the first disassembled PDF document is named ResultPDF1.pdf. For information about the shell_disassemble.xml DDX document used in this code example, see Programmatically Disassembling 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/"> 
    *  <PDFsFromBookmarks prefix="stmt"> 
    *    <PDF source="AssemblerResultPDF.pdf"/> 
    *</PDFsFromBookmarks> 
    *</DDX> 
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using DisassembleDocument.ServiceReference1; 
 
namespace DisassembleDocument 
{ 
       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 a BLOB object that represents the input DDX file and  
                   //the PDF document to disassemble 
                   BLOB ddxDoc = new BLOB(); 
                   BLOB inDoc = new BLOB(); 
 
                   //Get the input DDX document and input PDF document to disassemble 
                   string ddxFileName = "C:\\shell_disassemble.xml"; 
                   FileStream ddxFs = new FileStream(ddxFileName, FileMode.Open); 
                   string pdfFileName = "C:\\AssemblerResultPDF.pdf"; 
                   FileStream pdfDocIS = new FileStream(pdfFileName, FileMode.Open); 
 
                   //Get the file stream lengths and create byte arrays 
                   int ddxLen = (int)ddxFs.Length; 
                   byte[] ddxByteArray = new byte[ddxLen]; 
                   int pdfLen = (int)pdfDocIS.Length; 
                   byte[] pdfByteArray = new byte[pdfLen]; 
 
                   //Populate the byte arrays with the contents of the file streams 
                   ddxFs.Read(ddxByteArray, 0, ddxLen); 
                   pdfDocIS.Read(pdfByteArray, 0, pdfLen); 
 
                   //Populate the BLOB objects 
                   ddxDoc.MTOM = ddxByteArray; 
                   inDoc.MTOM = pdfByteArray; 
 
                   // 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 = "AssemblerResultPDF.pdf"; 
                   inMapDoc.value = inDoc; 
                   inputMap.Add(inMapDoc); 
     
                   //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 documents from the returned map 
                   BLOB outDoc = null; 
                   DisassembleDocument.ServiceReference1.Map mapResult = result.documents; 
                   for (int i = 0; i < mapResult.Count; i++) 
                   { 
                       //Determine the data type of the map item element 
                       if (mapResult[i].value is BLOB) 
                       { 
                           //Save the disassembled PDF document as  
                           //a PDF file 
                           outDoc = (BLOB)(mapResult[i].value); 
                           byte[] outByteArray = outDoc.MTOM; 
                           string FILE_NAME = "C:\\ResultPDF" + i + ".pdf"; 
                           FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                           BinaryWriter w = new BinaryWriter(fs2); 
                           w.Write(outByteArray); 
                           w.Close(); 
                           fs2.Close(); 
                       } 
                   } 
     
                   if (mapResult.Count > 0) 
                        Console.WriteLine("The PDF document was disassembled into "+mapResult.Count+" PDF documents."); 
                   else 
                       Console.WriteLine("The PDF document was not disassembled.");    
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
}