Quick Start: Invoking a service using DIME in a .NET project

The following C# code example invokes a process named MyApplication/EncryptDocument from a Microsoft .NET project using Dime. (See Invoking LiveCycle using Base64 encoding.)

An unsecured PDF document based on a PDF file named map.pdf is passed to the LiveCycle process using DIME. The process returns a password-encrypted PDF document that is saved as a PDF file named mapEncrypt.pdf.

/** 
    *  
    * Ensure that you create a .NET project that uses  
    * Web Services Enhancements 2.0. This is required to send a  
    * LiveCycle process an attachment using DIME. 
    *  
    * For information, see "Invoking LiveCycle using DIME" in Programming with LiveCycle.   
    */ 
 
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.IO; 
using Microsoft.Web.Services2.Dime; 
using Microsoft.Web.Services2.Attachments; 
using Microsoft.Web.Services2.Configuration; 
using Microsoft.Web.Services2; 
 
//The following statement represents a web reference to 
//the LiveCycle Server that contains the process that 
//is invoked 
using ConsoleApplication1.LC_Host; 
 
namespace ConsoleApplication1 
{ 
 
     class InvokeEncryptDocumentUsingDime 
       { 
 
           const int BUFFER_SIZE = 4096; 
           [STAThread] 
           static void Main(string[] args) 
           { 
 
           try 
              { 
                  String pdfFile = "C:\\Adobe\map.pdf"; 
                  String encryptedPDF = "C:\\Adobe\mapEncrypt.pdf"; 
     
                  //Create an EncryptDocumentServiceWse object and set authentication values 
                  EncryptDocumentServiceWse encryptClient = new EncryptDocumentServiceWse(); 
                  encryptClient.Credentials = new System.Net.NetworkCredential("administrator", "password"); 
 
                  // Create the DIME attachment representing a PDF document 
                  DimeAttachment inputDocAttachment = new DimeAttachment( 
                      System.Guid.NewGuid().ToString(), 
                      "application/pdf", 
                      TypeFormat.MediaType, 
                      pdfFile); 
     
                   //Create a BLOB object 
                   BLOB inDoc = new BLOB(); 
     
                   //Set the DIME attachment ID 
                   inDoc.attachmentID = inputDocAttachment.Id; 
                   encryptClient.RequestSoapContext.Attachments.Add(inputDocAttachment); 
     
                   //Invoke the EncryptDocument process 
                   BLOB outDoc = encryptClient.invoke(inDoc); 
     
                   //Get the returned attachment identifier value     
                   String encryptedDocId = outDoc.attachmentID; 
                   FileStream myStream = new FileStream(encryptedPDF, FileMode.Create, FileAccess.Write); 
     
                   //Iterate through the attachments 
                   foreach (Attachment attachment in encryptClient.ResponseSoapContext.Attachments) 
                     { 
                       if (attachment.Id.Equals(encryptedDocId)) 
                         { 
                           //Create a byte array that contains the encrypted PDF document 
                           System.IO.Stream mySteam2 = attachment.Stream; 
                           byte[] myBytes = new byte[mySteam2.Length]; 
                           int size = (int)mySteam2.Length; 
                           mySteam2.Read(myBytes, 0, size); 
     
                           //Save the encrypted PDF document as a PDF file 
                           FileStream fs2 = new FileStream(encryptedPDF, FileMode.OpenOrCreate); 
     
                           //Create a BinaryWriter object 
                           BinaryWriter w = new BinaryWriter(fs2); 
                           w.Write(myBytes); 
                           w.Close(); 
                           fs2.Close(); 
                           Console.Out.WriteLine("Saved converted document at:" + encryptedPDF); 
                       } 
                  } 
               } 
               catch (Exception ee) 
              { 
                  Console.WriteLine(ee.Message); 
               } 
           } 
       } 
} 

// Ethnio survey code removed