0

I'm using pdf-lib in a Node.js project to fill out a secured PDF form that contains a radio button group with custom export values and appearance dictionaries. The PDF is encrypted (password-protected), but I can load and modify it fine. When I call .select() on the radio group, the underlying field value updates correctly, but the visual appearance does not change—it remains stuck on the default appearance.

Here's a minimal example:

const fs = require('fs');
const { PDFDocument } = require('pdf-lib');

async function fillForm() {
  // Load the encrypted PDF form (password: 'secret')
  const pdfBytes = fs.readFileSync('form.pdf');
  const pdfDoc = await PDFDocument.load(pdfBytes, { password: 'secret' });
  
  const form = pdfDoc.getForm();
  
  // Get the radio group field (custom export values: 'CreditCard', 'PayPal', 'WireTransfer')
  const paymentMethod = form.getRadioGroup('PaymentMethod');
  
  // Select the "CreditCard" option
  paymentMethod.select('CreditCard');
  
  // Attempt to update field appearances
  form.updateFieldAppearances();
  
  const pdfBytesUpdated = await pdfDoc.save();
  fs.writeFileSync('filled.pdf', pdfBytesUpdated);
}

fillForm();

Even though paymentMethod.select('CreditCard') updates the field value, the visual appearance remains unchanged—the radio button does not reflect the "CreditCard" selection. This happens even after calling form.updateFieldAppearances(). The PDF's radio button group uses custom appearance dictionaries for each option.

How can I force pdf-lib to update the appearance of a radio button group with custom export values in an encrypted PDF so that the selection is visually reflected? Has anyone encountered this issue with custom appearance dictionaries not being redrawn after a .select() call?

UPDATE: The documentation warns that pdf-lib doesn’t support modifying encrypted PDFs reliably, and while unencrypted files work fine, encrypted ones don’t. Since the file must stay encrypted, a workaround is suggested: decrypt the file, perform your edits, and then save it with a new password.

This is valuable if you have access to setting pdf permissions - if not, no answer is yet provided.

2
  • It definitely works on non-encrypted files, but unfortunately these ones need to stay encrypted - but needs to be modified without removing the encryption. Not even sure if this is possible - wanted to check if anyone knows a work around Commented Mar 15 at 18:17
  • Thanks for all the feedback, everyone. I’ve updated the post accordingly. I appreciate knowing that if you have access to the PDF permissions, decryption and re-encryption can be a valuable workaround. Unfortunately, my issue involves a client who has been entrenched in using encryption for ages and isn’t willing to change their old ways. If anyone has alternative suggestions—even if it’s outside of pdf-lib—I’d really appreciate it. Commented Mar 16 at 7:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.