最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Radio button group appearance not updating for custom export values - Stack Overflow

programmeradmin2浏览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.

发布评论

评论列表(0)

  1. 暂无评论