Quick Start (MTOM): Creating a PDF document based on fragments using the web service API

The following C# web service code example creates a PDF document that is based on a form design assembled by the Assembler service. The Assembler service assembles fragments located in multiple XDP files into a single form design. Application logic that invokes the Assembler service is located in a user-defined method named GetFormDesign. The non-interactive form is saved as a PDF file named Loan.pdf on the client computer. (See Creating PDF Documents Using Fragments.)

???/** 
    * 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 is the DDX file is used to assemble multiple XDP documents: 
    * <?xml version="1.0" encoding="UTF-8"?> 
    * <DDX xmlns="http://ns.adobe.com/DDX/1.0/"> 
    *    <XDP result="tuc018result.xdp"> 
    *       <XDP source="tuc018_template_flowed.xdp"> 
    *          <XDPContent insertionPoint="ddx_fragment" source="tuc018_contact.xdp" fragment="subPatientContact" required="false"/> 
    *          <XDPContent insertionPoint="ddx_fragment" source="tuc018_patient.xdp" fragment="subPatientPhysical" required="false"/> 
    *          <XDPContent insertionPoint="ddx_fragment" source="tuc018_patient.xdp" fragment="subPatientHealth" required="false"/> 
    *       </XDP> 
    *    </XDP>         
    * </DDX> 
    * 
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
 
//A reference to the Output service  
using CreatePDFromFragments.ServiceReference1; 
 
//A reference to the Assembler service  
using CreatePDFromFragments.ServiceReference2; 
 
namespace CreatePDFromFragments 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Because BLOB objects are used in both service references 
                   //it is necessary to fully-qualify the BLOB objects 
 
                   //Retrieve the XDP document from the Assembler service  
                   CreatePDFromFragments.ServiceReference2.BLOB xdpContent = GetFormDesign(); 
 
                   //Create a BLOB object associated with the Output service 
                   CreatePDFromFragments.ServiceReference1.BLOB formDesign = new CreatePDFromFragments.ServiceReference1.BLOB(); 
 
                   //Transfer the byte stream from one BLOB object to the  
                   //Output BLOB object 
                   formDesign.MTOM = xdpContent.MTOM; 
 
                   //Render the non-interactive form 
                   RenderNonInteractiveForm(formDesign); 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
           } 
 
           //Get the form design from the Assembler service  
           private static CreatePDFromFragments.ServiceReference2.BLOB GetFormDesign() 
           { 
               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 = 4000000; 
                   b.MaxBufferSize = 4000000; 
                   b.ReaderQuotas.MaxArrayLength = 4000000; 
 
                   // Create BLOBs that represents the input files required to assemble multiple XDP files 
                   CreatePDFromFragments.ServiceReference2.BLOB myDDX = new CreatePDFromFragments.ServiceReference2.BLOB(); 
                   CreatePDFromFragments.ServiceReference2.BLOB inSourceBLOB = new CreatePDFromFragments.ServiceReference2.BLOB(); 
                   CreatePDFromFragments.ServiceReference2.BLOB inFragment1BLOB = new CreatePDFromFragments.ServiceReference2.BLOB(); 
                   CreatePDFromFragments.ServiceReference2.BLOB inFragment2BLOB = new CreatePDFromFragments.ServiceReference2.BLOB(); 
 
                   // Get the input DDX document and XDP documents to assemble 
                   string ddxFileName = "C:\\Adobe\fragmentDDX.xml"; 
                   FileStream ddxFs = new FileStream(ddxFileName, FileMode.Open); 
 
                   string inSource = "C:\\Adobe\tuc018_template_flowed.xdp"; 
                   FileStream inSourceFileFS = new FileStream(inSource, FileMode.Open); 
 
                   string inFragment1Source = "C:\\Adobe\tuc018_contact.xdp"; 
                   FileStream inFragment1SourceFS = new FileStream(inFragment1Source, FileMode.Open); 
 
                   string inFragment2Source = "C:\\Adobe\tuc018_patient.xdp"; 
                   FileStream inFragment2SourceFS = new FileStream(inFragment2Source, FileMode.Open); 
 
                   // Get the lengths of the file streams and create byte arrays 
                   int ddxLen = (int)ddxFs.Length; 
                   byte[] ddxByteArray = new byte[ddxLen]; 
 
                   int inSourceFileLen = (int)inSourceFileFS.Length; 
                   byte[] inSourceFileByteArray = new byte[inSourceFileLen]; 
 
                   int inFragment1SourceLen = (int)inFragment1SourceFS.Length; 
                   byte[] inFragment1SourceByteArray = new byte[inFragment1SourceLen]; 
 
                   int inFragment2SourceLen = (int)inFragment2SourceFS.Length; 
                   byte[] inFragment2SourceByteArray = new byte[inFragment2SourceLen]; 
 
                   // Populate the byte arrays with the contents of the file streams 
                   ddxFs.Read(ddxByteArray, 0, ddxLen); 
                   inSourceFileFS.Read(inSourceFileByteArray, 0, inSourceFileLen); 
                   inFragment1SourceFS.Read(inFragment1SourceByteArray, 0, inFragment1SourceLen); 
                   inFragment2SourceFS.Read(inFragment2SourceByteArray, 0, inFragment2SourceLen); 
 
                   // Populate the BLOB objects 
                   myDDX.MTOM = ddxByteArray; 
                   inSourceBLOB.MTOM = inSourceFileByteArray; 
                   inFragment1BLOB.MTOM = inFragment1SourceByteArray; 
                   inFragment2BLOB.MTOM = inFragment2SourceByteArray; 
 
                   // Create the map containing the PDF source documents 
                   CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType inputMap = new CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType(); 
 
                   CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item inSourceDoc = new CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inSourceDoc.key = "tuc018_template_flowed.xdp"; 
                   inSourceDoc.value = inSourceBLOB; 
 
                   CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item inFragment1SourceDoc = new CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inFragment1SourceDoc.key = "tuc018_contact.xdp"; 
                   inFragment1SourceDoc.value = inFragment1BLOB; 
 
                   CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item inFragment2SourceDoc = new CreatePDFromFragments.ServiceReference2.MyMapOf_xsd_string_To_xsd_anyType_Item(); 
                   inFragment2SourceDoc.key = "tuc018_patient.xdp"; 
                   inFragment2SourceDoc.value = inFragment2BLOB; 
 
                   //Add all elements to the collection 
                   inputMap.Add(inSourceDoc); 
                   inputMap.Add(inFragment1SourceDoc); 
                   inputMap.Add(inFragment2SourceDoc); 
 
                   // Create an AssemblerOptionsSpec object 
                   AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
                   assemblerSpec.failOnError = false; 
 
                   // Send the request to the Assembler Service 
                   AssemblerResult result = assembleClient.invoke(myDDX, inputMap, assemblerSpec); 
 
                   // Extract the newly created PDF document from the returned map 
                   CreatePDFromFragments.ServiceReference2.BLOB outDoc = null; 
                   CreatePDFromFragments.ServiceReference2.Map mapResult = result.documents; 
                   for (int i = 0; i < mapResult.Count; i++) 
                   { 
                       String myKey = (String)(mapResult[i].key); 
 
                       if (myKey == "tuc018result.xdp") 
                       { 
                           outDoc = (CreatePDFromFragments.ServiceReference2.BLOB)(mapResult[i].value); 
                       } 
                   } 
 
                   return outDoc;  
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
               return null; 
           } 
 
           //Render the non-interactive form -- invoke the Output service 
           private static void RenderNonInteractiveForm(CreatePDFromFragments.ServiceReference1.BLOB pdfForm) 
           { 
               try 
               { 
                   //Create an OutputServiceClient object 
                   OutputServiceClient outputClient = new OutputServiceClient(); 
                   outputClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/OutputService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)outputClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   outputClient.ClientCredentials.UserName.UserName = "administrator"; 
                   outputClient.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 to store form data 
                   CreatePDFromFragments.ServiceReference1.BLOB inData = new CreatePDFromFragments.ServiceReference1.BLOB(); 
 
                   //Reference XML data to merge into the PDF document 
                   string inputFileName = "C:\\Adobe\Loan.xml"; 
                   FileStream fs = new FileStream(inputFileName, FileMode.Open); 
 
                   //Get the length of the file stream and create a byte array  
                   int len = (int)fs.Length; 
                   byte[] byteArray = new byte[len]; 
 
                   //Populate the byte array with the contents of the file stream 
                   fs.Read(byteArray, 0, len); 
 
                   //Populate the BLOB object 
                   inData.MTOM = byteArray; 
 
                   //Set PDF run-time options 
                   PDFOutputOptionsSpec pdfOptions = new PDFOutputOptionsSpec(); 
                   pdfOptions.fileURI = "C:\\Adobe\Loan.pdf"; 
 
                   //Set rendering run-time options 
                   RenderOptionsSpec renderOptions = new RenderOptionsSpec(); 
                   renderOptions.cacheEnabled = true; 
                   renderOptions.PDFAConformance = PDFAConformance.A; 
                   renderOptions.PDFARevisionNumber = PDFARevisionNumber.Revision_1; 
                   pdfOptions.lookAhead = 300; 
                   pdfOptions.recordLevel = 1; 
 
                   //Create empty objects to pass as output arguments 
                   //to the generatePDFOutput2 method 
                   CreatePDFromFragments.ServiceReference1.BLOB generatePDFOutputMetaDataDoc = new CreatePDFromFragments.ServiceReference1.BLOB(); 
                   CreatePDFromFragments.ServiceReference1.BLOB generatePDFOutputResultDoc = new CreatePDFromFragments.ServiceReference1.BLOB(); 
                   OutputResult outResult = new OutputResult(); 
 
                   //Create a non-interactive document - invoke the generatePDFOutput2 method 
                  outputClient.generatePDFOutput2( 
                           TransformationFormat.PDF, 
                           "C:\\Adobe", 
                           pdfForm, 
                           pdfOptions, 
                           renderOptions, 
                           inData, 
                           out generatePDFOutputMetaDataDoc, 
                           out outResult); 
 
                   //Save the PDF document as a PDF file 
                   byte[] outByteArray = outResult.generatedDoc.MTOM; 
                   string FILE_NAME = "C:\\Adobe\Loan.pdf"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
 
                   //Create a BinaryWriter object 
                   BinaryWriter w = new BinaryWriter(fs2); 
                   w.Write(outByteArray); 
                   w.Close(); 
                   fs2.Close(); 
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
           } 
       } 
} 

// Ethnio survey code removed