Skip to content
Advertisement

Radiobutton display problems with PDFBox

I used the code from the answer from this question to create my radiobuttons: How to Create a Radio Button Group with PDFBox 2.0

After I created my PDF and tried to read the (programatically) selected value from it, this code worked fine:

JavaScript

When I open the PDF in Acrobat Reader DC, make changes and save it again the code doesn’t work anymore. There is no instance of PDRadioButton anymore and the value is always an empty string. enter image description here

When I open the PDF in Acrobat Touch it doesn’t even display properly. enter image description here

(When I open the version that was previously edited by Acrobat Reader DC, Acrobat Touch can display it correctly)

Any suggestions what may be wrong with the code?

Here is a minimal example that behaves the same:

JavaScript

Advertisement

Answer

Your code displays the top of the field tree. The javadoc of getFields() warns about this:

JavaScript

To get all the fields (this includes non terminal fields), do this:

JavaScript

Then your radio button appears.

However there are still another problem. The choice is returned as “a”, “b” or “Choice1” instead of “c”.

I was able to fix that by adding this code segment before adding the widget:

JavaScript

It adds empty appearances for “Off” and for the on-Option to each button.

Update 17.1.2017:

Here’s source code to generate radio buttons with appearance streams:

JavaScript

If you want Adobe to generate the appearance streams (that’s the “gibberish” in the code), call setNeedAppearances(true) and remove the line widget.setAppearance(appearance);. If you open the file with Adobe and save it, the appearance streams will be generated, and that is where I got these from. You can see these with PDFDebugger if you look at the annotations, then AP and go down from there.

That’s also the strategy to use if you want to know the appearance stream content for bigger buttons.

Some time in the future PDFBox will generate the appearance streams for buttons. There is some math involved, see here or in the trunk source code in PDCircleAppearanceHandler.

Advertisement