Skip to content

Commit

Permalink
do not overwrite the AcroForm when it was already there
Browse files Browse the repository at this point in the history
refs #198
  • Loading branch information
vbuch committed Nov 14, 2023
1 parent ac61226 commit 4899dcf
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/placeholder-pdf-lib/src/pdflibAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SUBFILTER_ADOBE_PKCS7_DETACHED,
} from '@signpdf/utils';
import {
PDFArray, PDFHexString, PDFName, PDFNumber, PDFString, toHexStringOfMinLength,
PDFArray, PDFDict, PDFHexString, PDFName, PDFNumber, PDFString, toHexStringOfMinLength,
} from 'pdf-lib';

/**
Expand Down Expand Up @@ -95,12 +95,28 @@ export const pdflibAddPlaceholder = ({
pdfDoc.context.obj([widgetDictRef]),
);

// Reference the widget in the a new AcroForm
pdfDoc.catalog.set(
PDFName.of('AcroForm'),
pdfDoc.context.obj({
SigFlags: SIG_FLAGS.SIGNATURES_EXIST | SIG_FLAGS.APPEND_ONLY,
Fields: [widgetDictRef],
}),
// Add an AcroForm or update the existing one
let acroForm = pdfDoc.catalog.lookupMaybe(PDFName.of('AcroForm'), PDFDict);
if (typeof acroForm === 'undefined') {
// Need to create a new AcroForm
acroForm = pdfDoc.context.obj({Fields: []});
}

/**
* @type {PDFNumber}
*/
let sigFlags;
if (acroForm.has(PDFName.of('SigFlags'))) {
// Already has some flags, will merge
sigFlags = acroForm.get(PDFName.of('SigFlags'));
} else {
// Create blank flags
sigFlags = PDFNumber.of(0);
}
const updatedFlags = PDFNumber.of(
sigFlags.asNumber() | SIG_FLAGS.SIGNATURES_EXIST | SIG_FLAGS.APPEND_ONLY,
);
acroForm.set(PDFName.of('SigFlags'), updatedFlags);
const fields = acroForm.get(PDFName.of('Fields'));
fields.push(widgetDictRef);
};

0 comments on commit 4899dcf

Please sign in to comment.