Skip to main content
Participant
April 7, 2025
Question

How can I digitally sign a document using JavaScript?

  • April 7, 2025
  • 2 replies
  • 824 views

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?




2 replies

PDF Automation Station
Community Expert
Community Expert
April 7, 2025

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.

Participant
April 9, 2025

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?

PDF Automation Station
Community Expert
Community Expert
April 9, 2025

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.

Bernd Alheit
Community Expert
Community Expert
April 7, 2025

The .pfx file is the certificate (digital ID).

Participant
April 9, 2025

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.

Bernd Alheit
Community Expert
Community Expert
April 9, 2025

May be that this is not possible with a script.