Skip to main content
Participant
Apr 7, 2025 1:35 PM UTC
Question

How can I digitally sign a document using JavaScript?

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?




6 replies

PDF Automation Station
Community Expert
Community Expert
Apr 7, 2025 3:59 PM UTC

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.

Translate
Participant
Apr 9, 2025 11:58 AM UTC

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?

Translate
PDF Automation Station
Community Expert
Community Expert
Apr 9, 2025 4:57 PM UTC

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.

Translate
Bernd Alheit
Community Expert
Community Expert
Apr 7, 2025 1:52 PM UTC

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

Translate
Participant
Apr 9, 2025 12:18 PM UTC

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.

Translate
Bernd Alheit
Community Expert
Community Expert
Apr 9, 2025 12:57 PM UTC

May be that this is not possible with a script.

Translate