Skip to main content
Participant
September 16, 2024
Question

Digital Signature - Javascript - General Error: Opertion Failed

  • September 16, 2024
  • 1 reply
  • 551 views

I've used the below code for ~7 years, but have recently been getting the following error: "An error occurred: GeneralError: Operation failed.", in the console, but unfortunately, this is all the console shows, so I have been unable to troubleshoot and determine what is causing the error. All I know is that the signature field gets placed correctly, but the signature is not being applied.

 

Any help is appreciated.

 

Thanks!

 

/*
 * sdkAddSignature.js
 * 
 * 
 * Folder Javascript Created by Acrobat SDK.
 * This JavaScript sample shows you can programmatically sign a PDF document using
 * your digital ID file. 
 * 
 * As a sample, this file has included all the digital signature information
 * except the path and password for the digital ID file to be used.
 * When you are ready to sign a PDF, click the newly added "Add My Signature" 
 * menu item under the Edit>Acrobat SDK JavaScript menu. 
 * 
 * After you input the platform independent path and 
 * the password through a dialog, the program will create a digital
 * signature field in the top-left corner, and sign it with your data. The path 
 * and password are valid in a Acrobat session, so you can continue to sign more 
 * documents in the session without the input dialogs.
 * If you change the code to specify the path and password for the digital ID 
 * file to be used in this file, then when you click the menu item, the program will 
 * go automatically to sign PDFs without UI. 
 * 
 * Using the function SetUserPassword() and function SetUserDigitalIDPath() in this 
 * folder JavaScript code, you can call the JavaScript to quietly sign a PDF from other 
 * JavaScript code, or from a plug-in or IAC VB or VC program through function 
 * ExecuteThisScript( ). The VB and C Sample code is attached in end of this file.
 * 
 * A digital signature file ( DrTest.pfx ) is provided with the sample for your test
 * To use it, put it in a folder, and specify the proper DIpath ( e.g. /C/DrTest.pfx ).
 * Its password is "testpassword". 
 */

/*
 * Use of an object to emulate a unique namespace.
 *
 * Object literals act like global variables
 * defined within this particular namespace.
 */
if (typeof ACROSDK == "undefined")
	var ACROSDK = {};
	
/* 
 * password to use the digital signature
 *
 * to test the sample without user input, specify:
 * ACROSDK.sigUserPwd = "testpassword";
 */
ACROSDK.sigUserPwd = "password";

/* 
 * path to the digital signature file
 *
 * to test the sample without user input, specify:
 * ACROSDK.sigDigitalIDPath = "/C/DrTest.pfx";
 */
ACROSDK.sigDigitalIDPath = "/G/My Drive/Digital Certificate/Date_Name.pfx";

// other variables the user can modify 
ACROSDK.sigHandlerName = "Adobe.PPKLite";
ACROSDK.sigFieldname = "SignatureField";
ACROSDK.sigLocation = "City, State";
ACROSDK.sigContactInfo = "email";

if ( typeof sdkMenuItem == "undefined")
	var sdkMenuItem = false;
	
if (!sdkMenuItem) {
	sdkMenuItem = true;
	app.addSubMenu( { 
		cName:"ACROSDK:JSSubMenu", 
		cUser: "Acrobat SDK JavaScript", 
		cParent: "Edit", 
		nPos: 0
	});
}

// Add a menu item for AddSignature
app.addMenuItem( { 
	cName: "ACROSDK:AddSignatureFL", 
	cUser: "Add Digital Signature - FL", 
	cParent: "ACROSDK:JSSubMenu", 
   cEnable: "event.rc = (event.target != null);",
   cExec: "AddSignatureFL(event.target)"    
});


/**
 * main function
 */ 
function AddSignatureFL(doc)
{
	// if ACROSDK.sigDigitalIDPath is not spcified, ask for user input
	if(ACROSDK.sigDigitalIDPath == "UNKNOWN"){
		var cResponse = app.response({
				cQuestion: "Input your digital ID path:",
				cTitle: "Digital Signature",
				cDefault: "/C/DrTest.pfx",
		});
		
		if ( cResponse == null) {
			app.alert("No input.");
			return;
		}
		else
			SetUserDigitalIDPath(cResponse);
	}
		
	// if ACROSDK.sigUserPwd is not spcified, ask for user input
	if(ACROSDK.sigUserPwd == "UNKNOWN"){
		var cResponse = app.response({
				cQuestion: "Input your password:",
				cTitle: "Digital Signature",
				cDefault:  "testpassword",
		});
		
		if ( cResponse == null) {
			app.alert("No input.");
			return
		}
		else
			SetUserPassword(cResponse);
	}

	// create a new signature field
	var signatureField = AddSignatureFieldFL(doc);

	// sign it
	if(signatureField)	Sign(signatureField, ACROSDK.sigHandlerName);
}



/**
 * create a signature field in the upper left conner with name of ACROSDK.sigFieldname
 */
function AddSignatureFieldFL(doc)
{
	var inch=72;
	var aRect = doc.getPageBox( {nPage: 0} );
	aRect[0] += 14.26*inch; // from upper left hand corner of page.
	aRect[2] = aRect[0]+1.75*inch; // Make it 1.75 inch wide
	aRect[1] -= 3.81*inch;
	aRect[3] = aRect[1] - 0.6*inch; // and 0.6 inch high

	var sigField = null;
	try {
		sigField = doc.addField(ACROSDK.sigFieldname, "signature", this.pageNum, aRect );
	} catch (e) {
		console.println("An error occurred: " + e);
	}

	return sigField;
}

/**
 * define the Sign function as a privileged function
 */ 
Sign = app.trustedFunction (
    function( sigField, DigSigHandlerName )
	{
	  try {
		 app.beginPriv();
		var myEngine = security.getHandler(DigSigHandlerName); 
		myEngine.login( ACROSDK.sigUserPwd, ACROSDK.sigDigitalIDPath); 
		sigField.signatureSetSeedValue({appearanceFilter: "FL", flags: 256});
		sigField.signatureSign({oSig: myEngine, 
								bUI: false,
								oInfo: { password: ACROSDK.sigUserPwd,
										contactInfo: ACROSDK.sigContactInfo}
								});
		app.endPriv
	  } catch (e) {
			console.println("An error occurred: " + e);
	  }
	}
);

/** 
 * set a correct password for using the signature, so you can quietly sign a doc.
 */
function SetUserPassword(pwd)
{
	ACROSDK.sigUserPwd = pwd;
}

/** 
 * set path to the digital signature file
 */
function SetUserDigitalIDPath(idPath)
{
	ACROSDK.sigDigitalIDPath = idPath;
}

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 17, 2024

The error is occuring in one of the two try/catch blocks. Either adding the signatue field or adding the signature. Since the signature field is placed correctly, it must be in the Sign function. 

You could narrow it down by making the message more verbose by using some of the properties of the exception object besides just "e".  You could also add more console.println statements in the code to nail down the exact line. 

It's possible that the signature is out of date. 

To test this, walk through the process of manually applying the signature.  When you select the digital certificate, Acrobat will show the properties, which will indicate the expiration date. 

Going though this process will also tell you if there is another issue such as the file path to the signature file. 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jcardenAuthor
Participant
September 17, 2024

Thank you Thom.

 

I modified the error message code as you suggested which told me the error was on line 191, which was the contact info in the signatureSign method. I commented that out, which reverted the error to line 190, the password. Neither of these properties are incorrect.

 

I have no issue manually applying the digital signature, and the certificate is not expired. And the file path has been the same for the past 5-6 years when my company migrated to G drive. That said, I have tried changing the filepath to a C drive location as a test, but it made no difference.

Thom Parker
Community Expert
Community Expert
September 17, 2024

The error is on the signatureSign function, the whole function, not the contact info.  

Try setting the UI parameter to true.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often