basquang™ on clouds

November 5, 2010

Add Digital Signature to a PDF programmatically using Adobe SDK and Microsoft .NET

Filed under: Digital Signature,PDF — basquang @ 11:18 AM

Requirement:

– Adobe Professional Installed

– Adobe SDK downloaded

– Microsoft Visual Studio

– Valid Certificate file (.pfx). It means the .pfx file not yet expired.

Setup environment:

– Copy sdkAddSignature.js from SDK to Acrobat\Javascripts folder

– Optional: Create Your Own Test Certificate

Code C#

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("d:\\temp\\s.pdf"))
            {
                jso = gpddoc.GetJSObject();
                object[] param = new object[1];

                param[0] = "c:\\CATest.pfx";

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

                param[0] = "testpassword";
                con = jso.GetType().InvokeMember("SetUserPassword",
                BindingFlags.InvokeMethod, null, jso, param);                    
                param[0] = jso;

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

Code VB:

Dim gapp As Acrobat.CAcroApp
    Dim gpddoc As Acrobat.CAcroPDDoc
    Dim jso As Object
    gapp = CreateObject("acroexch.app")
    gpddoc = CreateObject("acroexch.pddoc")

    If gpddoc.Open("d:\temp\s.pdf") Then
      jso = gpddoc.GetJSObject()
      jso.SetUserPassword("testpassword")
      jso.SetUserDigitalIDPath("/C/CATest.pfx")
      jso.AddSignature(jso)
      gapp.Show()
    End If

basquang@hotmail.com

Create a free website or blog at WordPress.com.