Quick Start (MTOM): Applying a policy to a Word document using the web service API

The following C# code example applies a policy named Allow Copy to a Word document named Loan.doc. The policy set to which the policy is added is named Global Policy Set. The policy-protected document is saved as a DOC file named PolicyProtectedLoanDoc.doc. (See Applying Policies to Word 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 ApplyPolicyWord.ServiceReference1; 
 
namespace ApplyPolicyWord 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create a RightsManagementServiceClient object 
                   RightsManagementServiceClient rmClient = new RightsManagementServiceClient(); 
                   rmClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/RightsManagementService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)rmClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   rmClient.ClientCredentials.UserName.UserName = "administrator"; 
                   rmClient.ClientCredentials.UserName.Password = "password"; 
                   b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
 
                   //Create a BLOB that represents the Word document 
                   //to which a policy is applied 
                   BLOB inDoc = new BLOB(); 
 
                   //Reference the Word document to which a policy is applied 
                   string inputFileName = "C:\\Adobe\Loan.doc"; 
                   FileStream fs = new FileStream(inputFileName, 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; 
 
                   //Prepare output parameters 
                   string PolicyID; 
                   string DocumentID; 
                   string MimeType; 
 
                   //Apply a policy to a Word document named Loan.doc 
                   BLOB outDoc = rmClient.protectDocument( 
                       inDoc, 
                       "Loan.doc", 
                       "Global Policy Set", 
                       "Allow Copy", 
                       null, 
                       null, 
                       RMLocale.en, 
                       out PolicyID, 
                       out DocumentID, 
                       out MimeType); 
 
                   //Populate a byte array with the contents of the BLOB 
                   byte[] outByteArray = outDoc.MTOM; 
 
                   //Create a new file containing the policy-protected Word document 
                   string FILE_NAME = "C:\\Adobe\PolicyProtectedLoanDoc.doc"; 
                   FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate); 
                   BinaryWriter w = new BinaryWriter(fs2); 
                   w.Write(outByteArray); 
                   w.Close(); 
                   fs2.Close(); 
 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine(ee.Message); 
               } 
 
           } 
       } 
} 

// Ethnio survey code removed