pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Cannot read checkbox value correctly

Open jwd24 opened this issue 3 years ago • 4 comments

What were you trying to do?

I have a PDF file with multiple checkboxes in it and I wanted to check one of them (in particular, the Yes checkbox for question #3)

How did you attempt to do it?

Using the the Adobe Embed API library, I displayed the PDF in a browser, MANUALLY updated one of the checkboxes (ie. the Yes box for question 3) and saved the pdf. I want to be clear that I am NOT using the check() method to check the checkbox, I'm manually doing it via the browser.

What actually happened?

When I open the saved pdf using a standard browser, I can see that the box is checked. However, when I call the pdf-lib isChecked() method for this field (the field name is "3"), I get back a 'false' instead of 'true'.

What did you expect to happen?

I expected isChecked() to return 'true' since that checkbox is checked. I've attached a copy of the 'saved' PDF with question 3's checkbox checked. Submitted_Manual3.pdf

Note that issue only happens for questions 1, 2, 3, 4, 5 and 9 of the attached pdf. When I check any of the other checkboxes, the value read from isChecked() is 'true'.

this is how I'm reading the values of the fields: const fields_temp = pdfDoc2.getForm().getFields(); var form = pdfDoc2.getForm(); fields_temp.forEach(field => { const fieldname = field.getName(); if (field instanceof PDFLib.PDFCheckBox) { const checkboxfield = form.getCheckBox(fieldname); const value = checkboxfield.isChecked(); console.log('Checkbox field before save: ' + fieldname + ' = ' + value); } })

How can we reproduce the issue?

To reproduce, simply read the fields of the attached pdf via the isChecked() method.
Field 3 will come back as 'false' even though its checked.

Version

https://unpkg.com/pdf-lib/dist/pdf-lib.js

What environment are you running pdf-lib in?

Browser

Checklist

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

Additional Notes

I did look thru the existing issues, but didn't notice anything that would address this issue.

jwd24 avatar Feb 06 '23 21:02 jwd24

I am having similar issue but I am not able to check correct checkbox as multiple check boxes have same name but different export values.

satyajitnayk avatar Oct 06 '23 11:10 satyajitnayk

Same issue here. I have multiple checkboxes and one is checked manually while the other remain unchecked. After saving in the browser and processing the file with pdf-lib, isChecked() returns true on all of them.

A deeper investigation of the PDF export side showed that the checkboxes values were "On" while being displayed as "Off". Thus, pdf-lib is doing it's job correct.

faltenberg avatar Oct 15 '23 00:10 faltenberg

@jwd24 @faltenberg posted a solution on a similar issue here https://github.com/Hopding/pdf-lib/issues/1185 see if it works for you

mirkodrummer avatar Oct 16 '23 10:10 mirkodrummer

I have found the source for my issue (See also Stackoverflow). In short: checkboxes in AcroForm have two states that can be either "On" or "Off". One is the internal value that you would read out with isChecked() and the other is the visualization property of appearing as checked. In my case, both were out of sync due to mistake during the PDF generation. When you toggle the checkbox in the viewer, both properties are inverted accordingly. Maybe your bug is on the generation side of the PDF as well, @jwd24.

faltenberg avatar Oct 16 '23 10:10 faltenberg