How to create radio buttons inside a table?

I know how to create a table and I know how to add a radio field using Rectangle objects that define the position of the radio buttons on the page. What I don't know, is how to make sure that these radio buttons appear inside my table.

Posted on StackOverflow on Oct 30, 2015 by Vikash

Please take a look at the CreateRadioInTable example.

In this example, we create a PdfButtonFormField for the radio group and we add it after constructing and adding the table:

PdfButtonFormField group = PdfFormField.createRadioGroup(pdfDoc, "Language", "English");
Table table = new Table(2);
// add cells
table.addCell(cell);
doc.add(table);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
form.addField(group);

When we create the cells for the radio buttons, we set a renderer, for instance:

cell.setNextRenderer(new MyCellRenderer(cell, group, "english"));

The renderer looks like this:

private class MyCellRenderer extends CellRenderer {
    protected String value;
    protected PdfButtonFormField radioGroup;
    public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String fieldName) {
        super(modelElement);
        this.radioGroup = radioGroup;
        this.value = fieldName;
    }
    @Override
    public void draw(DrawContext drawContext) {
        PdfFormField.createRadioButton(drawContext.getDocument(), getOccupiedAreaBBox(), radioGroup, value);
    }
}

Click this link if you want to see how to answer this question in iText 5.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now