Sunday, May 18, 2008

Add digital signature in pdf using c#

Few days back, one of our client wanted to add digital signature in pdf using window application. Windows application which will run after scheduled time need to add the the signature. Windows application was written in c#.

To achieve above goal, Adobe Acrobat professional and Adobe SDK is required.

Before writing c# code we need to following configuration setting

copy sdkAddSignature.js from Adobe\Acrobat 8 SDK\Version2\JavaScriptSupport\samples\outsidepdf\addsignature to adobeprofession javascript folder

copy DrTest.pft from Adobe\Acrobat 8 SDK\Version2\JavaScriptSupport\samples\outsidepdf\addsignature to C:\ (anywhere you like to)

Add reference of Interop.Acrobat.dll from Acrobat SDK

we can modify the location of signature on pdf or even hide it modifying the AddSignatureField function in sdkAddSignature.js.

Here is the final c# code which will add signature on pdf

Type AcrobatCAcroAppType;
AcrobatCAcroAppType = Type.GetTypeFromProgID("AcroExch.app");
Acrobat.CAcroApp gapp = (Acrobat.CAcroApp)Activator.CreateInstance(AcrobatCAcroAppType);

Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroPDDoc gpddoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);

object jso ;

if(gpddoc.Open("c:\\s.pdf"))
{
jso = gpddoc.GetJSObject();
object[] param = new object[1];
param[0] = "testpassword";
object con = jso.GetType().InvokeMember("SetUserPassword",
BindingFlags.InvokeMethod , null, jso, param);

param[0] = "C:\\DrTest.pfx";

con = jso.GetType().InvokeMember("SetUserDigitalIDPath",
BindingFlags.InvokeMethod , null, jso, param);

param[0] = jso;

con = jso.GetType().InvokeMember("AddSignature",
BindingFlags.InvokeMethod , null, jso, param);

Happy Coding

:)

3 comments:

Preeti said...

good one

basquang said...

Hi! I'm using your code. But object con always return NULL. Can you help me why?

electronic signatures said...

Getting the same problem. I used the given code but object con is returning the NULL. Am i missing something ? Please help me..

Site Meter