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

The following C# code example digitally signs a PDF document that is based on a PDF file named LoanSig.pdf. The alias that is specified for the security credential is secure, and revocation checking is performed. Because no CRL or OCSP server information is specified, the server information is obtained from the certificate used to digitally sign the PDF document. The signed document is saved as a PDF file named LoanSigned.pdf. (See Digitally Signing 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   
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using SignDocument.ServiceReference1; 
 
namespace SignDocument 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create a SignatureServiceClient object 
                   SignatureServiceClient sigClient = new SignatureServiceClient(); 
                   sigClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/SignatureService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)sigClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   sigClient.ClientCredentials.UserName.UserName = "administrator"; 
                   sigClient.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 a PDF document to sign 
                   BLOB inDoc = new BLOB(); 
 
                   //Specify a PDF document to sign 
                   string path = "C:\\Adobe\LoanSig.pdf"; 
                   FileStream fs = new FileStream(path, FileMode.Open); 
 
                   //Get the length of the file stream  
                   int len = (int)fs.Length; 
                   byte[] ByteArray = new byte[len]; 
 
                   //Populate the byte array with the contents of the FileStream object 
                   fs.Read(ByteArray, 0, len); 
                   inDoc.MTOM = ByteArray; 
 
                   //Specify the name of the signature field 
                   string fieldName = "SignatureField1"; 
 
                   //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 
                   BLOB signedDoc = sigClient.sign( 
                       inDoc, 
                       fieldName, 
                       myCred, 
                       HashAlgorithm.SHA1, 
                       reason,  
                       location, 
                       contactInfo, 
                       appear, 
                       true, 
                       null, 
                       null, 
                       null); 
 
                   //Populate a byte array with a BLOB data 
                   byte[] outByteArray = signedDoc.MTOM; 
 
                   //Save the signed PDF document 
                   string fileName = "C:\\Adobe\LoanSigned.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); 
               } 
           } 
       } 
}