Quick Start (MTOM): Digitally signing a XFA-based Form using the web service API

The following C# code example signs an interactive form that is rendered by the Forms service. The client application has two service references. The BLOB instance that is associated with the Forms service belongs to the SignInteractiveForm.ServiceReference2 namespace. Likewise, the BLOB instance that is associated with the Signature service belongs to the SignInteractiveForm.ServiceReference1 namespace. The signed interactive form is saved as a PDF file named LoanXFASigned.pdf.

???/** 
    * 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   
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
 
//A reference to the Signature service  
using SignInteractiveForm.ServiceReference1; 
 
//A reference to the Forms service  
using SignInteractiveForm.ServiceReference2; 
 
namespace SignInteractiveForm 
{ 
       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 form -- invoke the Forms service 
                   SignInteractiveForm.ServiceReference2.BLOB formData = GetForm(); 
 
                   //Create a BLOB object associated with the Signature service 
                   SignInteractiveForm.ServiceReference1.BLOB sigData = new SignInteractiveForm.ServiceReference1.BLOB(); 
 
                   //Transfer the byte stream from one Forms BLOB object to the  
                   //Signature BLOB object 
                   sigData.MTOM = formData.MTOM; 
 
                   //Sign the Form -- invoke the Signature service 
                   SignForm(sigData); 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
           } 
 
           //Creates an interactive PDF form based on a XFA form - invoke the Forms service 
           private static SignInteractiveForm.ServiceReference2.BLOB GetForm() 
           { 
 
               try 
               { 
                   //Create a FormsServiceClient object 
                   FormsServiceClient formsClient = new FormsServiceClient(); 
                   formsClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/FormsService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)formsClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   formsClient.ClientCredentials.UserName.UserName = "administrator"; 
                   formsClient.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 to store form data 
                   SignInteractiveForm.ServiceReference2.BLOB formData = new SignInteractiveForm.ServiceReference2.BLOB(); 
                   SignInteractiveForm.ServiceReference2.BLOB pdfForm = new SignInteractiveForm.ServiceReference2.BLOB(); 
 
                   //Specify a XML form data 
                   string path = "C:\\Adobe\Loan.xml"; 
                   FileStream fs = new FileStream(path, FileMode.Open); 
 
                   //Get the length of the file stream  
                   int len = (int)fs.Length; 
                   byte[] ByteArray = new byte[len]; 
 
                   fs.Read(ByteArray, 0, len); 
                   formData.MTOM = ByteArray; 
 
                   //Specify a XML form data 
                   string path2 = "C:\\Adobe\LoanSigXFA.pdf"; 
                   FileStream fs2 = new FileStream(path2, FileMode.Open); 
 
                   //Get the length of the file stream  
                   int len2 = (int)fs2.Length; 
                   byte[] ByteArray2 = new byte[len2]; 
 
                   fs2.Read(ByteArray2, 0, len2); 
                   pdfForm.MTOM = ByteArray2; 
 
                   PDFFormRenderSpec renderSpec = new PDFFormRenderSpec(); 
                   renderSpec.generateServerAppearance = true; 
 
                   //Set out parameter values 
                   long pageCount = 1; 
                   String localValue = "en_US"; 
                   FormsResult result = new FormsResult(); 
 
                   //Render an interactive PDF form 
                   formsClient.renderPDFForm2( 
                       pdfForm, 
                       formData, 
                       renderSpec, 
                       null, 
                       null, 
                       out pageCount, 
                       out localValue, 
                       out result); 
 
                   //Write the data stream to the BLOB object 
                   SignInteractiveForm.ServiceReference2.BLOB outForm = result.outputContent; 
                   return outForm; 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
               return null; 
           } 
 
           //Sign the form -- invoke the Signature service 
           private static void SignForm(SignInteractiveForm.ServiceReference1.BLOB inDoc) 
           { 
 
               try 
               { 
                   //Create a SignatureServiceClient object 
                   SignatureServiceClient signatureClient = new SignatureServiceClient(); 
                   signatureClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/SignatureService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)signatureClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   signatureClient.ClientCredentials.UserName.UserName = "administrator"; 
                   signatureClient.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; 
 
                   //Specify the name of the signature field 
                   string fieldName = "form1[0].grantApplication[0].page1[0].SignatureField1[0]"; 
 
                   //Create a Credential object 
                   Credential myCred = new Credential(); 
                   myCred.alias = "secure"; 
 
                   //Specify the reason to sign the document 
                   string reason = "The document was reviewed"; 
 
                   //Specify the location of the signer 
                   string location = "New York HQ"; 
 
                   //Specify contact information 
                   string contactInfo = "Tony Blue"; 
 
                   //Create a PDFSignatureAppearanceOptions object  
                   //and show date information 
                   PDFSignatureAppearanceOptionSpec appear = new PDFSignatureAppearanceOptionSpec(); 
                   appear.showDate = true; 
 
                   //Sign the PDF document 
                   SignInteractiveForm.ServiceReference1.BLOB signedDoc = signatureClient.sign( 
                       inDoc, 
                       fieldName, 
                       myCred, 
                       HashAlgorithm.SHA1, 
                       reason, 
                       location, 
                       contactInfo, 
                       appear, 
                       true, 
                       null, 
                       null, 
                       null); 
 
                   //Populate a byte array with BLOB data that represents the signed form 
                   byte[] outByteArray = signedDoc.MTOM; 
 
                   //Save the signed PDF document 
                   string fileName = "C:\\Adobe\LoanXFASigned.pdf"; 
                   FileStream fs2 = new FileStream(fileName, 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); 
               } 
           } 
       } 
}