How to create radio buttons inside a table?

Tags: tablesformsradio buttonradio fieldiText 7

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.