Quick Start (MTOM mode): Dynamically creating a DDX document using the web service API

The following C# code example dynamally creates a DDX document that disassembles a PDF document. A new PDF document is created for each level 1 bookmark in the input PDF document. This code example contains the following user-defined methods:

  • CreateDDX: Creates a System.Xml.XmlDocument object that represents the DDX document that is sent to the Assembler service. This user-defined method returns a System.Xml.XmlDocument object.

  • convertDDX: Converts a System.Xml.XmlDocument object to a byte array. This method accepts a System.Xml.XmlDocument object as an input parameter and returns a byte array that is used to populate the BLOB object’s MTOM field.

    Both of these methods are invoked in this quick start. (See Dynamically Creating DDX 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 
        *  
        *  This quick start dynamically creates the following DDX document: 
        * <?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 CreateDDX.ServiceReference1; 
    using System.Xml;  
     
    namespace CreateDDX 
    { 
           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 PDF document to disassemble 
                       string pdfFileName = "C:\\AssemblerResultPDF.pdf"; 
                       FileStream pdfDocIS = new FileStream(pdfFileName, FileMode.Open); 
     
                       //Get the file stream length and create the byte array 
                       int pdfLen = (int)pdfDocIS.Length; 
                       byte[] pdfByteArray = new byte[pdfLen]; 
     
                       //Populate the byte array with the content of the file stream 
                        pdfDocIS.Read(pdfByteArray, 0, pdfLen); 
     
                       //Populate the BLOB objects 
                       XmlDocument ddx = CreateDDX(); 
                       ddxDoc.MTOM = ConvertDDX(ddx); //Call ConvertDDX 
                       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; 
                       CreateDDX.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); 
                   } 
               } 
     
           private static XmlDocument CreateDDX() 
           {  
               //This method dynamically creates a DDX document  
               //that is passed to the Assembler service. 
               //The XML is returned as a byte array 
               XmlDocument ddx = new XmlDocument(); 
     
               //Create the root element and append it to the XML DOM         
               System.Xml.XmlElement root = ddx.CreateElement("DDX"); 
               root.SetAttribute("xmlns", "http://ns.adobe.com/DDX/1.0/"); 
               ddx.AppendChild(root); 
     
               //Create the PDFsFromBookmarks element 
               XmlElement PDFsFromBookmarks = ddx.CreateElement("PDFsFromBookmarks"); 
               PDFsFromBookmarks.SetAttribute("prefix", "stmt"); 
               root.AppendChild(PDFsFromBookmarks); 
     
               //Create the PDF element 
               XmlElement PDF = ddx.CreateElement("PDF"); 
               PDF.SetAttribute("source", "AssemblerResultPDF.pdf"); 
               PDFsFromBookmarks.AppendChild(PDF); 
         
               //Return the XmlElement instance 
               return ddx; 
           } 
     
     
           //Convert the XmlDocument to a byte array 
           private static byte[] ConvertDDX(XmlDocument ddx) 
               { 
               //Populate a MemoryStream object with DDX data 
               MemoryStream stream = new MemoryStream(); 
               ddx.Save(stream); 
         
               //Convert the stream to a byte array 
               int bufLen = Convert.ToInt32(stream.Length); 
               byte[] byteArray = new byte[bufLen]; 
               stream.Position = 0;  
               int count = stream.Read(byteArray, 0, bufLen); 
     
               //Return the byte array 
               return byteArray; 
           } 
          } 
    } 
    

// Ethnio survey code removed