//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2023 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- const { PDFNet } = require('@pdftron/pdfnet-node'); const PDFTronLicense = require('../LicenseKey/LicenseKey'); ((exports) => { 'use strict'; exports.runDigitalSignatureTest = () => { const input_path = '../../TestFiles/'; const output_path = '../../TestFiles/Output/'; const SignAndTSAPDF = async (in_docpath, in_approval_field_name, in_private_key_file_path, in_keyfile_password, in_appearance_img_path, in_timestamp_authority_root_certificate_path, in_outpath) => { const doc = await PDFNet.PDFDoc.create(); let page = await doc.pageCreate(); await doc.pagePushBack(page); const digsig_field = await doc.createDigitalSignatureField(in_approval_field_name); const widgetAnnot = await PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new PDFNet.Rect(143, 287, 219, 306), digsig_field); const img = await PDFNet.Image.createFromFile(doc, in_appearance_img_path); await widgetAnnot.createSignatureAppearance(img); await page.annotPushBack(widgetAnnot); // Create a digital signature dictionary inside the digital signature field, in preparation for signing. await digsig_field.createSigDictForCustomSigning("Adobe.PPKLite", PDFNet.DigitalSignatureField.SubFilterType.e_adbe_pkcs7_detached, 8200); // For security reasons, set the contents size to a value greater than but as close as possible to the size you expect your final signature to be, in bytes. await doc.save(in_outpath, PDFNet.SDFDoc.SaveOptions.e_incremental); const pdf_digest = await digsig_field.calculateDigest(PDFNet.DigestAlgorithm.Type.e_SHA256); const signature_value = await PDFNet.DigitalSignatureField.signDigestPath(pdf_digest, in_private_key_file_path, in_keyfile_password, false, PDFNet.DigestAlgorithm.Type.e_SHA256); await doc.saveCustomSignature(signature_value, digsig_field, in_outpath); // Add embedded timestamp to signature. const tst_config = await PDFNet.TimestampingConfiguration.createFromURL('http://rfc3161timestamp.globalsign.com/advanced'); const opts = await PDFNet.VerificationOptions.create(PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving); await opts.addTrustedCertificateUString(in_timestamp_authority_root_certificate_path); await opts.enableOnlineCRLRevocationChecking(true); const result = await digsig_field.generateContentsWithEmbeddedTimestamp(tst_config, opts); if (await result.getStatus()) { console.log('Saving...'); await doc.saveCustomSignature(await result.getData(), digsig_field, in_outpath); } else { console.log(await result.getString()); } console.log('================================================================================'); } const main = async () => { var ret = 0; console.log(await PDFNet.getVersionString()); try { await SignAndTSAPDF(input_path + 'waiver_withApprovalField.pdf', 'PDFTronApprovalSig', input_path + 'pdftron.pfx', 'password', input_path + 'pdftron.bmp', input_path + 'GlobalSignRootForTST.cer', output_path + 'waiver_withApprovalField_certified_output_TSA.pdf'); } catch (err) { console.log(err); ret = 1; } //////////////////// End of tests. //////////////////// if (!ret) { console.log('Tests successful.\n=========='); } else { console.log('Tests FAILED!!!\n=========='); } }; PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); }); }; exports.runDigitalSignatureTest(); })(exports); // eslint-disable-next-line spaced-comment //# sourceURL=DigitalSignatureTest.js