Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I digitally sign a document using JavaScript?

New Here ,
Apr 07, 2025 Apr 07, 2025

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?




TOPICS
JavaScript , Security digital signatures and esignatures
156
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2025 Apr 07, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 09, 2025 Apr 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2025 Apr 09, 2025

May be that this is not possible with a script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2025 Apr 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 09, 2025 Apr 09, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2025 Apr 09, 2025
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines