Multi-Sign and certify pdf

Product: PDFNet NodeJS

Product Version: 9.5.0-632868e89f

Please give a brief summary of your issue: Multi-Sign and certify pdf

Please describe your issue and provide steps to reproduce it:
I have a question for signing and certifying pdfs. I have the following requirement:
I have an existing pdf with placeholders (text marked with beginning “##” and ending “##”) which i need to search. If i have found one of these placeholders, i need to place a signatureField with an image and certifiy the given signatureField. This works perfectly. But the problem is, that i need to have multiple signatures on the same pdf with multiple certifications. So each signature needs to be signed with the same certificate. But this wont work. I can only add one signatureField with certification. If i would like to add a second signature and certify it, it will throw the following exception:

“Exception: Message: Cannot certify – certification already present in document
Conditional expression: IMPL_TRN_PDFDocGetDigitalSignaturePermissions(mp_field_dict_obj->GetDoc()) == e_unrestricted
Version : 9.5.0-632868e89f
Platform : Windows
Architecture : AMD64
Filename : DigitalSignatureField.cpp
Function : trn::PDF::DigitalSignatureField::CertifyOnNextSaveWithCustomHandler
Linenumber : 447”

Attached is my code. I haven’t found any examples on this requirement. Can you please give me an example on how to sign and certify a document multiple times?

const pdfBuffer = fs.readFileSync("test.pdf");
const signaturePng = fs.readFileSync("signature.png");
const doc = await PDFNet.PDFDoc.createFromBuffer(pdfBuffer);
const signatureImg = await PDFNet.Image.createFromMemory2(doc, signaturePng);

const pageCount = await doc.getPageCount();
for (let i = 1; i <= pageCount; i++) {
	const page = await doc.getPage(i);

	const txt = await PDFNet.TextExtractor.create();
	const rect = await page.getCropBox();
	txt.begin(page, rect); // Read the page.

	// Extract words one by one.
	let line = await txt.getFirstLine();
	while (await line.isValid()) {
		let word = await line.getFirstWord();
		while (await word.isValid()) {
			const wordString = await word.getString();
			if (wordString.startsWith("##")) {
				const bbox = await word.getBBox();

				/* Create a new signature form field in the PDFDoc. The name argument is optional; leaving it empty causes it to be auto-generated. However, you may need the name for later. Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible. */
				const certification_sig_field = await doc.createDigitalSignatureField(`signature-${wordString}`);
				const widgetAnnot = await PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new PDFNet.Rect(bbox.x1, bbox.y1 - 50, bbox.x1 + 100, bbox.y1 + 5), certification_sig_field);
				await page.annotPushBack(widgetAnnot);

				// (OPTIONAL) Add an appearance to the signature field.
				await widgetAnnot.createSignatureAppearance(signatureImg);

				await certification_sig_field.certifyOnNextSave(pathToCertificate, password);

				// (OPTIONAL) Add more information to the signature dictionary.
				await certification_sig_field.setLocation('Test, Test');
				await certification_sig_field.setReason('Test');
				await certification_sig_field.setContactInfo('Test');
			word = await word.getNextWord();
		line = await line.getNextLine();
await"signedAndCertified.pdf", 0);
1 Like

You can only have one certification/author signature per document, so you see your exception message when you attempt to add another. Here is a link that describes the different types of digital signatures that can be added using PDFNet and signing multiple fields:

1 Like