Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enableReadOnly() method does not work for checkboxes (PDFCheckBox) #1622

Open
2 tasks done
GarikEachbase opened this issue Apr 29, 2024 · 2 comments
Open
2 tasks done

Comments

@GarikEachbase
Copy link

GarikEachbase commented Apr 29, 2024

What were you trying to do?

I am trying to set fields as read only not to allow users chage it. field.enableReadOnly(). For PDFTextField fields it works fine, but for PDFCheckBox-es it does not work.

How did you attempt to do it?

const form = pdfDoc.getForm();
const fields = form.getFields();
fields.forEach((f) => f.enableReadOnly());

What actually happened?

For PDFTextField fields it works fine, but for PDFCheckBox-es it does not work.

What did you expect to happen?

I expect that user can't change checkboxes value but user can check and uncheck each checkbox

How can we reproduce the issue?

async fillFormFields(formData: Record<string, string | string[]>) {
const filePath = path.join(__dirname, 'templates', 'CMS1500.pdf');
const pdfBuffer = fs.readFileSync(filePath);
const pdfDoc = await PDFDocument.load(pdfBuffer);
const form = pdfDoc.getForm();
const fields = form.getFields();
// Apply permissions to make the document read-only
CMS1500(6).pdf

fields.forEach((f) => f.enableReadOnly());
for (const fieldName of Object.keys(formData)) {
  const field = fields.find((f) => f.getName() === fieldName);
  if (field) {
    let fieldValue = formData[fieldName];
    if (field instanceof PDFTextField) {
      if (!fieldValue) fieldValue = ' ';
      field.setText(fieldValue as string);
    } else if (field instanceof PDFCheckBox) {
      const isChecked = Boolean(fieldValue);
      if (isChecked) {
        field.check();
      } else {
        field.uncheck();
      }
      console.log(field);
    }
  }
}
const modifiedPdfBytes = await pdfDoc.save();
fs.writeFileSync(filePath, modifiedPdfBytes);

}

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

@GarikEachbase
Copy link
Author

1500Form (16).pdf

@CostGranda
Copy link

Try doing the readonly after setting the value, and you can print if the checkbox has been marked as dirty, otherwise mark it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants