Deletion of SignatureField still changes pdf doc

Product: pdfnet-node

Hi

I am working on signing/certificate pdfs and now want to remove already saved signings from the pdf.
I used the following code from the ticket “How to delete a specific digital signature field? - Technical Support / Apryse SDK - Apryse Community”.

The problem i have is, that it still changes the pdf-doc. What do I need to change, so that i can remove signatures without the pdf seems to think there are changes?

async function deleteSignatureFromProtocol() {
	const doc = await PDFNet.PDFDoc.createFromBuffer(this.pdfBuffer);
	const field = await doc.getField(this.signatureFieldName);
	const isFieldValid = await field.isValid();
	if (field == null || !isFieldValid) {
		// no field to delete
		return;
	}
	const fieldObj = await field.getSDFObj();
	const fieldObjNum = await fieldObj.getObjNum();

	if (this.hasSignatureWidget) {
		// remove SignatureWidget
		const widget = await PDFNet.SignatureWidget.createFromObj(fieldObj);
		let page = await widget.getPage();
		let widgetIndex = -1;
		if (await page.isValid()) {
			widgetIndex = await getIndexOfAnnotOnPage(page, fieldObjNum);
		}
		if (widgetIndex < 0) {
			const iterator = await doc.getPageIterator()
			for (iterator; iterator.hasNext(); iterator.next()) {
				const currentObj = await iterator.current();
				widgetIndex = await getIndexOfAnnotOnPage(currentObj, fieldObjNum);
				if (widgetIndex >= 0) {
					page = await iterator.current();
					break;
				}
			}
		}
		if (widgetIndex >= 0 && await page.isValid()) {
			await page.annotRemoveByIndex(widgetIndex);
		}
	}

	// remove the CertificateField
	let formsObj = null;
	const acroformsObj = await doc.getAcroForm();
	if (acroformsObj != null && await acroformsObj.isDict()) {
		formsObj = await acroformsObj.findObj("Fields");
	}
	if (formsObj != null && await formsObj.isArray()) {
		for (let i = 0; i < await formsObj.size(); ++i) {
			const formObj = await formsObj.getAt(i);
			if (await formObj.getObjNum() == fieldObjNum) {
				await formsObj.eraseAt(i);
				break;
			}
		}
	}
	return await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_incremental);

The same problem occurs when i add signatures to the pdf. The signatures are all valid but my pdf-reader points out the following statement: “The version of the document that was signed has not been changed. However, the following changes were made to the document.”

This is how i add the signature:

async function certificateAndSignProtocol() {
    let signedProtocolBuffer = this.pdfBuffer;
	const p12CertBuffer = base64Converter.decode(this.base64P12Cert);
	const doc = await PDFNet.PDFDoc.createFromBuffer(this.pdfBuffer);
	const pageCount = await doc.getPageCount();
	for (let i = 1; i <= pageCount; i++) {
		const page = await doc.getPage(i);

		const textExtractor = await PDFNet.TextExtractor.create();
		const pageCropBox = await page.getCropBox();
		textExtractor.begin(page, pageCropBox);

		let line = await textExtractor.getFirstLine();
		while (await line.isValid()) {
			let word = await line.getFirstWord();
			while (await word.isValid()) {
				const wordAsString = await word.getString();
				if (wordAsString.startsWith(`###${this.signatureId}###`)) {
					const bbox = await word.getBBox();
					const signatureField = await doc.createDigitalSignatureField(`Signature_${this.signatureId}`);

					const img = await PDFNet.Image.createFromMemory2(doc, this.signatureImageBuffer);
					const signatureWidget = await PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new PDFNet.Rect(bbox.x1, bbox.y1 - 50, bbox.x1 + 100, bbox.y1 + 5), signatureField);
					await signatureWidget.createSignatureAppearance(img);
					await page.annotPushBack(signatureWidget);

					await signatureField.signOnNextSaveFromBuffer(p12CertBuffer, this.certSecret);
					if (this.signatureReason) {
						await signatureField.setReason(this.signatureReason);
					}
					await signatureField.setContactInfo(this.signatureContactInfo);
					signedProtocolBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_incremental);
				}
				word = await word.getNextWord();
			}
			line = await line.getNextLine();
		}
	}
	return signedProtocolBuffer;
}

Thank you for posting your question to our forum. We will provide you with an update as soon as possible.

Hello,

The original forum page specifically mentions:

Note, if any of the signature, except perhaps the target one, are already signed, then this action will invalidate the other signed signature(s). If they other signatures are not signed, then this action should be fine.

This is because removing the signature constitutes as a change and will require re-signing.