From 78c1b3de882201262af268a0bfc8d5d30e9ad2e9 Mon Sep 17 00:00:00 2001 From: Valery Buchinsky Date: Tue, 14 Nov 2023 11:26:16 +0200 Subject: [PATCH] do not overwrite page annotations when there already were some refs #198 --- .../src/pdflibAddPlaceholder.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/placeholder-pdf-lib/src/pdflibAddPlaceholder.js b/packages/placeholder-pdf-lib/src/pdflibAddPlaceholder.js index a80fd321..52fd91a6 100644 --- a/packages/placeholder-pdf-lib/src/pdflibAddPlaceholder.js +++ b/packages/placeholder-pdf-lib/src/pdflibAddPlaceholder.js @@ -45,7 +45,7 @@ export const pdflibAddPlaceholder = ({ subFilter = SUBFILTER_ADOBE_PKCS7_DETACHED, widgetRect = [0, 0, 0, 0], }) => { - const pages = pdfDoc.getPages(); + const page = pdfDoc.getPage(0); // Create a placeholder where the the last 3 parameters of the // actual range will be replaced when signing is done. @@ -85,15 +85,17 @@ export const pdflibAddPlaceholder = ({ V: signatureDictRef, T: PDFString.of('Signature1'), F: ANNOTATION_FLAGS.PRINT, - P: pages[0].ref, + P: page.ref, }, pdfDoc.index); const widgetDictRef = pdfDoc.context.register(widgetDict); // Annotate the widget on the first page - pages[0].node.set( - PDFName.of('Annots'), - pdfDoc.context.obj([widgetDictRef]), - ); + let annotations = page.node.lookupMaybe(PDFName.of('Annots'), PDFArray); + if (typeof annotations === 'undefined') { + annotations = pdfDoc.context.obj([]); + } + annotations.push(widgetDictRef); + page.node.set(PDFName.of('Annots'), annotations); // Add an AcroForm or update the existing one let acroForm = pdfDoc.catalog.lookupMaybe(PDFName.of('AcroForm'), PDFDict);