Quick Start (MTOM): Assembling an encrypted PDF document using the web service API

The following C# code example assembles a password-encrypted PDF document. The unsecured PDF document is named Loan.pdf. Notice that the name of the DDX document is shell_Encrypt.xml. The encrypted PDF document is named AssemblerEncryptedPDF.pdf. The encrypted PDF document is named AssemblerEncryptedPDF.pdf. (See Assembling Encrypted 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  
    *  
    * * The following XML represents the DDX document used in this quick start: 
    *<?xml version="1.0" encoding="UTF-8"?> 
    *  <DDX xmlns="http://ns.adobe.com/DDX/1.0/"> 
    *   <PDF result="EncryptLoan.pdf" encryption="userProtect"> 
    *        <PDF source="inDoc" /> 
    *    </PDF> 
    *    <PasswordEncryptionProfile name="userProtect" compatibilityLevel="Acrobat7"> 
    *        <OpenPassword>AdobeOpen</OpenPassword> 
    *   </PasswordEncryptionProfile> 
    * </DDX> 
    */ 
     
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using EncryptDocument.ServiceReference1; 
 
namespace EncryptDocument 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               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 = 2000000; 
                   b.MaxBufferSize = 2000000; 
                   b.ReaderQuotas.MaxArrayLength = 2000000; 
     
                   // Create BLOB objects that represents the input  
                   // DDX file and the unsecured PDF document 
                   BLOB ddxDoc = new BLOB(); 
                   BLOB LoanDoc = new BLOB(); 
 
                   // Get the input DDX document and input PDF sources 
                   string ddxFileName = "C:\\shell_Encrypt.xml"; 
                   FileStream ddxFs = new FileStream(ddxFileName, FileMode.Open); 
                   string pdfFileName = "C:\\Adobe\Loan.pdf"; 
                   FileStream loanFs = new FileStream(pdfFileName, FileMode.Open); 
 
                   // Get the file stream lengths and create byte arrays 
                   int ddxLen = (int)ddxFs.Length; 
                   byte[] ddxByteArray = new byte[ddxLen]; 
                   int loanLen = (int)loanFs.Length; 
                   byte[] loanByteArray = new byte[loanLen]; 
 
                   // Populate the byte arrays with the contents of the file streams 
                   ddxFs.Read(ddxByteArray, 0, ddxLen); 
                   loanFs.Read(loanByteArray, 0, loanLen); 
 
                   // Populate the BLOB objects 
                   ddxDoc.MTOM = ddxByteArray; 
                   LoanDoc.MTOM = loanByteArray; 
 
                   // Create an AssemblerOptionsSpec object 
                   AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec(); 
                   assemblerSpec.failOnError = false; 
 
                   // Send the request to the Assembler Service 
                   BLOB outDoc = assembleClient.invokeOneDocument(ddxDoc, LoanDoc, assemblerSpec); 
 
                   //Populate a byte array with BLOB data 
                   byte[] outByteArray = outDoc.MTOM; 
 
                   //Create a new file containing the returned encrypted PDF document 
                   string FILE_NAME = "C:\\AssembleEncryptedPDF.pdf"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                   BinaryWriter w = new BinaryWriter(fs2); 
                   w.Write(outByteArray); 
                   w.Close(); 
                   fs2.Close(); 
 
                   Console.WriteLine("The PDF file was encrypted."); 
               } 
 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
}