Search code examples
pdfacrofields

Problem implementing PDF radio button fields


I'm currently in the process of implementing support for AcroForm for HexaPDF. Check boxes are already working fine but radio buttons give me a headache.

I created a simple PDF with two radio buttons. As far as I can tell the resulting PDF contains the needed objects and is structured according to the the PDF 1.7 specification. However, at least in Adobe Reader and Evince clicking on the radio buttons does nothing.

From the PDF:

  • There is the radio button field (object 6):

    <<
      /FT /Btn
      /T (Radio)
      /V /card1
      /Ff 98304
      /Kids [7 0 R 8 0 R ]
    >>
    
  • And then there are the two widget annotations (object 7 and 8, only 7 is shown):

    <<
      /Type /Annot
      /Subtype /Widget
      /Rect [200 500 250 550 ]
      /AP <<
        /N <<
          /card1 10 0 R
          /Off 9 0 R
        >>
      >>
      /MK <<
        /BC [0.0 ]
        /BG [1.0 ]
        /CA (l)
      >>
      /BS <<
        /W 1
        /S /S
      >>
      /Parent 6 0 R
      /AS /card1
      /F 4
    >>
    

Comparing the provided file to PDF files created with other tools didn't lead me to any change I could make to the radio buttons so that they work correctly.

I would appreciate any pointers as to what I'm doing wrong - thanks!


Solution

  • Turns out the field flags /Ff were wrongly set due to the age old "off by one" indexing error. The PDF specification uses 1-based indices in the bit descriptions, Ruby uses 0-based indices when accessing individual bits of an integer.

    Therefore the "radio" bit was not set and thus the buttons couldn't work because they weren't radio buttons.

    After changing the indexing the PDF can now be viewed fine in the viewers.