Skip to main content
Participating Frequently
February 21, 2008
Question

Add Digital Signature Using C# and Acrobat SDK

  • February 21, 2008
  • 56 replies
  • 47925 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 25, 2008
PDL, I searched a lot in the internet and in the Acrobat Documentation to how use the acrobat sdk and javascript to sign a document using a certificate stored in a smartcard.
When I use a smart card (PKCS#11 Device) inside the Acrobat, it recognises the smart card and I can sign de document using the card (no PFX certificates are needed).
When I plug the card in my pc, appears in the "Windows Certificate Store" the certificate that are stored in the card. So, using Javascript I found the serial number of the certicate stored in the smart card (I'm sure that the serial number is correct), and I tried to use it and Javascript to sign a pdf document, like below:

var myEngine = security.getHandler("Adobe.PPKLite");
var oCerts = myEngine.digitalIDs.certs;
var serial = "XXXXXX"; //serial number of the certificat that I am looking for in the "Windows Cert Store"
var iSerial;

//search in the windows store certificate for a certificate with the serial number informed
for (var i=0; i < oCerts.length; i++)
{
if (oCerts.serialNumber == serial)
i = iSerial;
}

console.println("Signing using the certificate: "+oCerts[iSerial]);

myEngine.login( {oParams:{oEndUserSignCert: oCerts[iSerial] , cPassword: "MyPassword"}});
var f = this.getField("SignHere");

f.signatureSign( myEngine,
{password: "MyPassword",// provide password
location: "Local",
reason: "Any reason",
contactInfo: "555-5555",
appearance: "MyApperance"});

The code above can't loggin the certificate, the problem is that the password is ignored by the code, and I don't know why.
I did a test, if I pass a path of any other PFX certificate in my pc instead of the object certificate (oCerts[iSerial]) the code above works fine: read the password and can loggin the certificate.

Any idea, please??

Thanks all in advance!
Patrick_Leckey
Participating Frequently
March 15, 2008
If it's compatible with Acrobat, you'll need to research the crypto provider functions (they are all thoroughly documented in the JS API Reference). You are already using one of these methods in your current code, it is just referencing the default Adobe crypto provider.

If your hardware is not compatible with Acrobat (i.e. you can't manually use it within the Acrobat UI) then you will need to use it's own APIs, which has nothing to do with the Acrobat SDK and we would not be able to help you here.
Participating Frequently
March 14, 2008
I did a C# application that signs a document. It signs using "PFX" certificates.
Please, somebody knows how can I sign a document using a "ID Card" (accessible via PKCS#11 Devices)?
When I use a PKCS#11 Device inside the Acrobat, it recognises the device and I can sign de document using the device (no PFX certificates are needed). How can I sign a PDF document using PKCS#11 Devices and C#? How to recognise that the device is plugged in my pc? I think that I will need to change my javascript code that signs, but I have no ideias how I will work with this. Some help, please??

Thank you very much again!
Participating Frequently
March 14, 2008
I am not expert in Acrobat, but I think that the only way is buying the Professional or the Standard version. But there are many 3rd party tools that you can use to sign. You can try iTextSharp for example, it works well with C# and is free. There are many samples in the internet of how use it.
Participating Frequently
March 14, 2008
thanx for reply AAndi

I wanted to know is there any adobe plug in or dll which i can use to sign document rather than buying Adobe Acrobat Professional or Acrobat Standard
Participating Frequently
March 14, 2008
>I wanted to know is there any adobe plug in ... i can use to sign document rather than buying Adobe Acrobat Professional or Acrobat Standard

But plug-ins require Acrobat, so where does that get us? I think the
answer is going to be no - for programmatic signing, at least. Reader
can manually sign Reader enabled files.


Aandi Inston
Participating Frequently
March 14, 2008
Hi Aandi,

Thanks alot for reply.

If i m not wrong you mean to say i need to buy Adobe Acrobat Professional or Acrobat Standard to sign the document.

Thankx
sumit
Participating Frequently
March 14, 2008
What alternative could there be? I don't get what you are looking for.

Aandi Inston
Participating Frequently
March 14, 2008
Hi PDL/Felipe,

Do I need to buy Adobe Acrobat Professional to sign the document or there is some other adobe plug in comes to sign the document.

My requirement is just to sign the document.

Thankx
Sumit
Participating Frequently
March 14, 2008
>Do I need to buy Adobe Acrobat Professional

The Acrobat SDK is for automating Acrobat. No plug-in can change that
basic idea. Most of the SDK works with Acrobat Standard too.

Aandi Inston
Patrick_Leckey
Participating Frequently
March 11, 2008
I do not use Sumit.d's method which uses InvokeMember, I call my functions directly. You would have to research the InvokeMember method (which is not part of the Acrobat SDK, it is C#.NET) and see how it handles return values from the member functions it invokes.

Again, I find it much easier to just call the functions directly.
Participating Frequently
March 11, 2008
PDL, I tried what you said but it is not working, please, could you take a look if I am doing something wrong?:

I have this javascript function:

Sign = app.trustedFunction (function( sigField, DigSigHandlerName )
{
try
{
var status;
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);
status = e;
return status;
}
}
);

I am returning "status" as a error, but I can't catch this in my C# application. I am using the Sumit's C# code, like below:

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

object jso ;
STRING STATUS;
if(gpddoc.Open("c:\\MyFile.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;

STATUS = (jso.GetType().InvokeMember("AddSignature",
BindingFlags.InvokeMethod , null, jso, param)).TOSTRING;
MESSAGEBOX.SHOW(STATUS)

Please, atenttion to the text in the upper case, I am trying to cacth the exception, but "status" always are null.
Do you see any mistake?

Thank you PDL!!
Patrick_Leckey
Participating Frequently
March 11, 2008
Use a try/catch in your JavaScript and in the catch section have the function return a value of your choice. That will be the return value of executing your function in your .NET app.