How can I digitally sign a document using JavaScript?
Copy link to clipboard
Copied
As part of a bigger product I need to programatically sign some PDFs using Adobe Acrobat. I have a physical USB token connected, and manually I would do the following:
"Use a certificate" -> "Digitally sign" -> draw the field with the mouse -> select the Digital ID from the list (only one) -> "Continue" -> "Sign" -> "save" on the windows pop up
I have come up with the following code to set up the signature field and try to sign:
var pageWidth = 612;
var pageHeight = 792;
var signatureWidth = 100;
var signatureHeight = 50;
var padding = 10;
var left = pageWidth - signatureWidth - padding;
var bottom = pageHeight - signatureHeight - padding;
var f = this.addField("mySignature", "signature", 0, [left, bottom, left + signatureWidth, bottom + signatureHeight]);
var ppklite = security.getHandler("Adobe.PPKLite");
var sigField = this.getField("mySignature");
sigField.signatureSign(ppklite);
but the response I get is:
Exception in line 13 of function top_level, script Console:Exec
GeneralError: Operation failed.
Field.signatureSign:13:Console undefined:Exec
[ Creation of this signature could not be completed. ] -> [ You have not selected a valid digital ID. Try again. ]
After trying to figure out using the oficial documentation and some unofficial examples, I have to say I am stumped, as most examples use the `login` function by passing a .pfx file, which I do not know of existing. Any ideas as to how I can automate this process I can do manually?
Copy link to clipboard
Copied
The .pfx file is the certificate (digital ID).
Copy link to clipboard
Copied
Ah, that mekes sense. As it turns out my certificate is non-exportable as a .pfx, since it is stored on a physical USB. Is there no other way to achieve this automation besides using the login method with a .pfx file? There is clearly some communication established between Adobe and my physical certificate since I can manually sign documents.
Copy link to clipboard
Copied
May be that this is not possible with a script.
Copy link to clipboard
Copied
Look at the field method signatureSign in the reference:
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#id652
You are missing parameters to fully automate this.
Copy link to clipboard
Copied
I have read the documentation on signatureSign, and from what I can see all other fields besides `oSig` are optional, so I simply ommited them as I do not require any extra information besides what the certificate contains. If my thinking is wrong, could you please go into a bit more detail as to what other fields I should include?
Copy link to clipboard
Copied
You are correct. All parameters are optional. Look at the example in the documentation though. The security handler includes the login:
var myEngine = security.getHandler("Adobe.PPKLite");
myEngine.login( "dps017", "/c/profile/dps.pfx" );
"dps017" is the password and "/c/profile/dps.pfx" is the path to the digital ID. It can only be executed from a privileged context.

