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

The following C# code example encrypts a PDF document named Loan.pdf with a password value of OpenPassword. The master password is PermissionPassword. The secured PDF document is saved as a PDF file named EncryptLoan.pdf. (See Encrypting PDF Documents with a Password.)

/** 
    * 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 EncryptDocumentPassword.ServiceReference1; 
 
namespace EncryptDocumentPassword 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create an 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; 
                   b.MaxReceivedMessageSize = 2000000; 
                   b.MaxBufferSize = 2000000; 
                   b.ReaderQuotas.MaxArrayLength = 2000000; 
 
                   //Create a BLOB object to store the PDF document  
                   BLOB inDoc = new BLOB(); 
 
                   //Specify the PDF document to encrypt with a password 
                   string path = "C:\\Adobe\Loan.pdf"; 
                   FileStream fs = new FileStream(path, FileMode.Open); 
 
                   //Get the file stream length and create a byte array   
                   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 PasswordEncryptionOptionSpec  
                   //object that stores encryption run-time values 
                   PasswordEncryptionOptionSpec passSpec = new PasswordEncryptionOptionSpec(); 
 
                   //Specify the PDF document resource to encrypt 
                   passSpec.encryptOption = PasswordEncryptionOption.ALL; 
 
                   //Specify the Acrobat version 
                   passSpec.compatability = PasswordEncryptionCompatability.ACRO_7; 
 
                   //Specify the password values 
                   passSpec.documentOpenPassword = "OpenPassword"; 
                   passSpec.permissionPassword = "PermissionPassword"; 
 
                   //Encrypt the PDF document with a password 
                   BLOB outDoc = encryptClient.encryptPDFUsingPassword(inDoc, passSpec); 
 
                   //Populate a byte array with a BLOB data 
                   byte[] outByteArray = outDoc.MTOM; 
 
                   //Create a new file that represents the encrypted PDF document 
                   string FILE_NAME = "C:\\Adobe\EncryptLoan.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); 
               } 
 
           } 
       } 
}