Search code examples
pdfbox

Why are my checkboxes disappearing when I flatten the PDF?


I am trying to flatten my PDF's using the PDAcroForm flatten method. The pdf I create is based on the example code for PDFBox Checkbox creation (I end up with a single page with one checkbox). However, when I call the flatten method on the PDF's Acroform, the checkbox I created disappears. Any idea what might be causing this?

Using PDFBox version: 2.0.5


Solution

  • Answer:

    This bug was occurring when I would create unchecked checkboxes.

    After reading another StackOverflow answer by Matyas, the solution was that I needed to explicitly set the value of the checkbox as unchecked.

    I was able to verify the solution by viewing the actual PDF Structure. When I wasn't explicitly setting the checkbox value, the new checkboxes that I added onto a PDF didn't have a /V value (whether the checkbox is checked or unchecked). When I explicitly set the value to unchecked (which adds the /V value to /Off), then the flattening method didn't erase the checkboxes.

    Please see an updated example in PDFBOX Checkbox Creation to get the proper implementation.