Quick Start (MTOM): Encrypting a PDF document with a certificate using the web service API

The following C# code example encrypts a PDF document named Loan.pdf with a certificate named Encryption.cer. The encrypted PDF document is saved as a PDF file named EncryptLoanCert.pdf. (See Encrypting PDF Documents with Certificates.)

    * 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 PKIEncryptDocument.ServiceReference1; 
namespace PKIEncryptDocument 
       class Program 
           static void Main(string[] args) 
                   //Create a EncryptionServiceClient object 
                   EncryptionServiceClient encryptClient = new EncryptionServiceClient(); 
                   encryptClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/EncryptionService?blob=mtom"); 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)encryptClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   encryptClient.ClientCredentials.UserName.UserName = "administrator"; 
                   encryptClient.ClientCredentials.UserName.Password = "password"; 
                   b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
                   //Create a BLOB object to store the PDF document  
                   BLOB inDoc = new BLOB(); 
                   //Specify the PDF document to encrypt 
                   string path = "C:\\Adobe\Loan.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; 
                   //Create a Recipient object to store certificate information 
                   Recipient recipient = new Recipient(); 
                   //Create a BLOB object to store certificate information  
                   BLOB cert = new BLOB(); 
                   //Specify the location of the certificate 
                   string certPath = "C:\\Adobe\Encryption.cer"; 
                   FileStream fsCert = new FileStream(certPath, FileMode.Open); 
                   //Get the length of the file stream  
                   int lenCert = (int)fsCert.Length; 
                   byte[] cerByteArray = new byte[lenCert]; 
                   //Populate the byte array with the contents of the FileStream object 
                   fsCert.Read(cerByteArray, 0, lenCert); 
                   cert.MTOM = cerByteArray; 
                   //Assign the certificate to the Recipient object 
                   recipient.x509Cert = cert; 
                   //Create a CertificateEncryptionIdentity object 
                   CertificateEncryptionIdentity encryptionId = new CertificateEncryptionIdentity(); 
                   encryptionId.recipient = recipient; 
                   //Create an Object array that is passed to the encryptPDFUsingCertificates method 
                   MyArrayOfCertificateEncryptionIdentity cerInfo = new  MyArrayOfCertificateEncryptionIdentity(); 
                   //Create a CertificateEncryptionOptionSpec 
                   //object that stores encryption run-time values 
                   CertificateEncryptionOptionSpec pkiSpec = new CertificateEncryptionOptionSpec(); 
                   //Specify the PDF document resource to encrypt 
                   pkiSpec.option = CertificateEncryptionOption.ALL; 
                   //Specify the Acrobat version 
                   pkiSpec.compat = CertificateEncryptionCompatibility.ACRO_7; 
                   //Encrypt the PDF document with a certificate 
                   BLOB outDoc = encryptClient.encryptPDFUsingCertificates(inDoc, cerInfo, pkiSpec); 
                   //Populate a byte array with BLOB data - use the MTOM field 
                   byte[] outByteArray = outDoc.MTOM; 
                   //Create a new file that represents the encrypted PDF document 
                   string FILE_NAME = "C:\\Adobe\EncryptLoanCert.pdf"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                   //Create a BinaryWriter object 
                   BinaryWriter w = new BinaryWriter(fs2); 
               catch (Exception ee) 

// Ethnio survey code removed