Hi,
Please find my code below. You need to call the ApplyDigitalSignatureOnFile function on 'the button click event. We apply the digital signature when the user clicks the Signature button, which is an external button. In this sample, when the user performs an e-signature using the signature panel from web viewer UI, we create a corresponding digital signature field for each e-sign. The certificate is then applied when the user clicks the Digital Signature button , and user can add others annotation like shapes, stamp , measurement …etc as well from UI. After applied digital signature we save the updated file in the database. It is working fine in 11.5.0 but not working in 11.6.0 and all latest version
$(document).ready(function () {
WebImageViewer();
});
var globalDocumentViewer = '', globalAnnotationManager = '';
var globalinstance = '';
var signatureCounter = 1;
var newlyAnnotationArray = [];
function WebImageViewer() {
//Initiate the webviewer.
WebViewer(
{
path: "../../../js/lib/PdfTronWebViewer11.8.0/",
preloadWorker: 'pdf',
initialDoc: 'https://example.com/file.pdf',
licenseKey: 'licenseKey',
fullAPI: true,
enableMeasurement: true,
isAdminUser: true,
signature: {
revocationTimeout: 2000,
}
}, viewer
).then(instance => {
globalinstance = instance;
instance.UI.enableFeatures([instance.UI.Feature.Initials]);
const { VerificationOptions, openElements, loadDocument } = instance.UI;
const { PDFNet, documentViewer } = instance.Core;
const { annotationManager, Annotations } = instance.Core;
const { WidgetFlags } = Annotations;
globalDocumentViewer = documentViewer;
annotationManager.addEventListener('annotationChanged', (annotations, action) => {
if (action === 'add') {
if (annotations !== null && annotations.length == 1 && annotations[0].Subject == "Signature") {
addSignatureToWidget(annotations[0]);
}
}
});
// Function to add signature image to the widget
const addSignatureToWidget = async (widgetAnnotation) => {
signatureCounter++;
widgetAnnotation.SignFieldName = "_digitalsign_" + signatureCounter;
widgetAnnotation.Id = "_esign_" + signatureCounter;
newlyAnnotationArray.push(widgetAnnotation);
};
});
}
var digitalSignatureFieldArray = '';
const ApplyDigitalSignatureOnFile = async () => {
const { VerificationOptions, openElements, loadDocument } = globalinstance.UI;
const { PDFNet, documentViewer } = globalinstance.Core;
const { annotationManager, Annotations } = globalinstance.Core;
const { WidgetFlags } = Annotations;
// Sets the entire document to be read-only
const doc = await documentViewer.getDocument().getPDFDoc();
var idCounter = 0;
for (var i = 0; i < newlyAnnotationArray.length; i++) {
idCounter = newlyAnnotationArray[i].Id.split('_')[2];
// set flags for required
const flags = new WidgetFlags();
flags.set('Required', false);
// create a form field
const field = new Annotations.Forms.Field( "_digitalsignField_" + idCounter, {
type: 'Sig',
flags,
});
// create a widget annotation
const widgetAnnot = new Annotations.SignatureWidgetAnnotation(field, {
appearance: '_DEFAULT',
appearances: {
_DEFAULT: {
Normal: {
offset: {
x: 100,
y: 100,
},
},
},
},
});
// set position and size
widgetAnnot.PageNumber = newlyAnnotationArray[i].PageNumber;
widgetAnnot.X = newlyAnnotationArray[i].X;
widgetAnnot.Y = newlyAnnotationArray[i].Y;
widgetAnnot.Width = newlyAnnotationArray[i].Width;
widgetAnnot.Height = newlyAnnotationArray[i].Height;
widgetAnnot.Id = "_digitalsign_" + idCounter;
//add the form field and widget annotation
annotationManager.getFieldManager().addField(field);
annotationManager.addAnnotation(widgetAnnot, { autofocus: true });
annotationManager.drawAnnotationsFromList([widgetAnnot]);
}
newlyAnnotationArray = [];
const xfdfString = await annotationManager.exportAnnotations({
useDisplayAuthor: true,
});
const mergeOptions = new PDFNet.PDFDoc.MergeXFDFOptions();
mergeOptions.setForce(true);
await doc.mergeXFDFString(xfdfString, mergeOptions);
var updatedBytesArray = '';
var password = 'certificatePassword';
try {
// Run PDFNet methods with memory management
await PDFNet.runWithCleanup(async () => {
doc.initSecurityHandler();
doc.lock();
digitalSignatureFieldArray = '';
for (const fitr = await doc.getDigitalSignatureFieldIteratorBegin(); await fitr.hasNext(); await fitr.next()) {
const approvalSigField = await fitr.current();
if (!await approvalSigField.hasCryptographicSignature()) {
await approvalSigField.setDocumentPermissions(PDFNet.DigitalSignatureField.DocumentPermissions.e_annotating_formfilling_signing_allowed);
await approvalSigField.signOnNextSaveFromURL(certificatePath, password);
updatedBytesArray = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_incremental);
}
}
const blob = new Blob([updatedBytesArray], { type: 'application/pdf' });
newlyAnnotationArray = [];
globalinstance.UI.loadDocument(blob, { filename: 'myfile.pdf', extension: "pdf" });
});
}
catch (err) {
}
}
Please let me know If you need anything else.?