Skip to main content
Participating Frequently
February 21, 2008
Question

Add Digital Signature Using C# and Acrobat SDK

  • February 21, 2008
  • 56 replies
  • 47957 views
Hi everybody!

Please, how can I digitally sign PDF documents using Acrobat Professional 8 API and C# language?
I know that I need use JavaScript APIs (IAC) but I can't find anything really helpful in the Acrobat SDK Documentation. Could anyone post a sample of how use javascript manipulation inside C# (Framework 2.0), or give me a direction?

Thank in advance!
This topic has been closed for replies.

56 replies

Participating Frequently
March 11, 2008
Sumit.d, or anybody that can help, Is it possible to catch the exceptions that may occur in the javascript execution, to show in my .NET application?

Let me explain better with a example. When the user try to sign a document using my C# application and he type a wrong password, the document is not signed, but we just see the error opening the javascript debugger, inside the Acrobat Professional. Is it possible catch the error message to show in my c# application?
Participating Frequently
March 10, 2008
Thank you very much Sumit.d, much better than VB.NET!!
It works fine!!

Thank you!!
Participating Frequently
March 10, 2008
Hi Felipe,

Here is the code in 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("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);
Participating Frequently
March 6, 2008
As I said before, I couldn't use C#. I think that there is differences between your VB.NET code and mine code. Try my VB code:

Private Sub ExecuteJavaScript()
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("c:YourFile.pdf") Then
jso = gpddoc.GetJSObject()
jso.SetUserPassword("testpassword")
jso.SetUserDigitalIDPath("/C/DrTest.pfx")
jso.AddSignature(jso)
gapp.Show()
End If

End Sub

Now, about your javascript code, I think that is the same I used. The javascript code that I used is exactly the same that came with Acrobat SDK Documentation (sdkAddSignature.js file).
Participating Frequently
March 6, 2008
Hi PDL/Felipie,

I could sign the PDF using VB.NET code.

Can you please help me to convert this code in C#.

Here is my code:

Type AcrobatCAcroAppType;
AcrobatCAcroAppType = Type.GetTypeFromProgID("AcroExch.PDDoc");
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();
jso.SetUserPassword("testpassword");
jso.SetUserDigitalIDPath("C:\\DrTest.pfx");
jso.AddSignature(jso);
}

I get error in following lines :

jso.SetUserPassword("testpassword");
jso.SetUserDigitalIDPath("C:\\DrTest.pfx");
jso.AddSignature(jso);

Thanks
sumit
Participating Frequently
March 6, 2008
Hi Felipe,

when i am signing the document.

I believe i am missing something in my code. After executing the code to sign, when i open it i get "The following signature fields are not signed".

Please let me know where i m doing wrong?

Here is my VB.NET Code

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("C:\Test1.pdf") Then
jso = gpddoc.GetJSObject()

jso.SetUserPassword("'testpassword12'")
'jso.ShowMyMessage("SetUserDigitalIDPath")
jso.SetUserDigitalIDPath("'c:\\DrTest.pfx'")
jso.app.execMenuItem("ADBESDK:AddSignature")
jso.AddSignature(jso)
gapp.Show()

and here is the javascript

// password to use the digital signature
var sigUserPwd = "UNKNOWN";
// to test the sample without user input, specify:
// var sigUserPwd = "testpassword";

// path to the digital signature file
var sigDigitalIDPath = "UNKNOWN";
// to test the sample without user input, specify:
//var sigDigitalIDPath = "/C/DrTest.pfx";

// other variables the user can modify
var sigHandlerName = "Adobe.PPKLite";
var sigFieldname = "sdkSignatureTest";
var sigReason = "I want to test my digital signature program.";
var sigLocation = "San Jose, CA";
var sigContactInfo = "sendme@testinc.com";

/* Add a menu item for AddSignature */
app.addMenuItem( { cName: "ADBESDK:AddSignature", cUser: "Add My Signature", cParent: "Advanced",
cEnable: "event.rc = (event.target != null);",
cExec: "AddSignature(event.target)" });

// main function
AddSignature=app.trustedFunction(function (doc)
{
app.beginPriv(); // explicitly raise privilege
// if sigDigitalIDPath is not spcified, ask for user input
if(sigDigitalIDPath == "UNKNOWN"){
var cResponse = app.response({
cQuestion: "Input your digital ID path:",
cTitle: "Digital Signature",
cDefault: "/C/DrTest.pfx",
});

if ( cResponse == null) {
app.alert("No input.");
return;
}
else
SetUserDigitalIDPath(cResponse);
}

// if sigUserPwd is not spcified, ask for user input
if(sigUserPwd == "UNKNOWN"){
var cResponse = app.response({
cQuestion: "Input your password:",
cTitle: "Digital Signature",
cDefault: "testpassword",
});

if ( cResponse == null) {
app.alert("No input.");
return
}
else
SetUserPassword(cResponse);
}

// create a new signature field
var signatureField = AddSignatureField(doc);

// sign it
if(signatureField) Sign(signatureField, sigHandlerName);
app.endPriv();
})

// create a signature field in the upper left conner with name of sigFieldname
function AddSignatureField(doc)
{
var inch=72;
var aRect = doc.getPageBox( {nPage: 0} );
aRect[0] += 0.5*inch; // from upper left hand corner of page.
aRect[2] = aRect[0]+2*inch; // Make it 2 inch wide
aRect[1] -= 0.5*inch;
aRect[3] = aRect[1] - 0.5*inch; // and 0.5 inch high

var sigField = null;
try {
sigField = doc.addField(sigFieldname, "signature", 0, aRect );
} catch (e) {
console.println("An error occurred: " + e);
}

return sigField;
}

// define the Sign function as a privileged function
Sign = app.trustedFunction (
function( sigField, DigSigHandlerName )
{
try {
app.beginPriv();
var myEngine = security.getHandler(DigSigHandlerName);
myEngine.login( sigUserPwd, sigDigitalIDPath);
sigField.signatureSign({oSig: myEngine,
bUI: false,
oInfo: { password: sigUserPwd,
reason: sigReason,
location: sigLocation,
contactInfo: sigContactInfo}
});
app.endPriv
} catch (e) {
console.println("An error occurred: " + e);
}
}
);

// set a correct password for using the signature, so you can quietly sign a doc.
function SetUserPassword(pwd)
{
sigUserPwd = pwd;
}

// set path to the digital signature file
function SetUserDigitalIDPath(idPath)
{
sigDigitalIDPath = idPath;
}

Sumit
Participating Frequently
March 4, 2008
Sumit is open wrong.. He is using my old example, that is wrong.

Sumit, try to convert my last VB.NET post to C#, you don't need to open twice.
Patrick_Leckey
Participating Frequently
March 4, 2008
Why are you opening the file twice?
Participating Frequently
March 4, 2008
Sumit, here I am specifying better my problem:
http://www.adobeforums.com/webx/.3c064c08/1
Participating Frequently
March 4, 2008
hi PDL

Here is the code i m using in C#

Acrobat.AcroAVDoc acroAVDoc = new Acrobat.AcroAVDoc();
acroAVDoc.Open(@"c:\T-form.pdf", "TempTitle");

Acrobat.CAcroPDDoc acroDoc = (Acrobat.CAcroPDDoc)acroAVDoc.GetPDDoc();
acroDoc.Open(@"c:\T-form.pdf");
Object jso = acroDoc.GetJSObject();

but i am not getting any method AddSignature in jso