Is it possible to add checkbox/radiobutton on table cell?

I want to add checkbox and radiobutton on table columns using aspose.pdf in java.

@Patil3412

Please check below code sample to add radio buttons in table cells. You can add check box as well using same approach:

Document document = new Document();

Page page = document.getPages().add();
RadioButtonField rf = new RadioButtonField(page);
rf.setPartialName("radio");

Table table = new Table();
table.setColumnWidths("100 20 100 20 100");
Rectangle pageRect = page.getRect();
table.setTop((float) pageRect.getHeight()-200-15);
table.setLeft(170);
page.getParagraphs().add(table);

Row r1 = table.getRows().add();
Cell c1 = r1.getCells().add();
Cell c2 = r1.getCells().add();
Cell c3 = r1.getCells().add();
Cell c4 = r1.getCells().add();
Cell c5 = r1.getCells().add();

RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
RadioButtonOptionField opt3 = new RadioButtonOptionField();

opt1.setOptionName("Item1");
opt2.setOptionName("Item2");
opt3.setOptionName("Item3");

opt1.setWidth(15);
opt1.setHeight(15);
opt2.setWidth(15);
opt2.setHeight(15);
opt3.setWidth(15);
opt3.setHeight(15);


rf.add(opt1);
rf.add(opt2);
rf.add(opt3);
document.getForm().add(rf, 1);

opt1.setBorder(new Border(opt1));
opt1.getBorder().setWidth(1);
opt1.getBorder().setStyle(BorderStyle.Solid);
opt1.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
opt1.getDefaultAppearance().setTextColor(java.awt.Color.RED);
opt1.setCaption(new TextFragment("Item1"));
opt2.setBorder(new Border(opt2));
opt2.getBorder().setWidth(1);
opt2.getBorder().setStyle(BorderStyle.Solid);
opt2.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
opt2.getDefaultAppearance().setTextColor(java.awt.Color.RED);
opt2.setCaption(new TextFragment("Item2"));
opt3.setBorder(new Border(opt3));
opt3.getBorder().setWidth(1);
opt3.getBorder().setStyle(BorderStyle.Solid);
opt3.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
opt3.getDefaultAppearance().setTextColor(java.awt.Color.RED);
opt3.setCaption(new TextFragment("Item3"));

c1.getParagraphs().add(opt1);
c3.getParagraphs().add(opt2);
c5.getParagraphs().add(opt3);


document.save(dataDir + "PDFJAVA_39580_output.pdf");
1 Like

@asad.ali
Thank you for the response, I would like to ask can we pre-select radiobutton/checkboxfield programmatically and make pdf as read-only so no one should able to edit manually.

@Patil3412

You can set Checked property of the filed to make it pre-checked. Also, you can flatten the PDF document before saving with Document.Flatten() method to make it read-only.

@asad.ali
Hi,
I am unable to make radiobutton pre-selected programmatically.
rf.setSelected(rf.getOptions().get(“1”).getIndex()); // used setSelected method.
Please suggest different method if any.

@Patil3412

Please try using the ActiveState property and set it same as the value of the radio button e.g. YES, NO and let us know in case issue still persists. Please share the complete code snippet that you have used so far. We will further proceed to assist you accordingly.

Hi @asad.ali,
Can you please tell how to set name/caption to checkboxfield?. I am not able to set name/caption to checkboxfield using existing methods but nor working.

@Patil3412

Can you please share what do you actually mean by name/caption? Do you want to access the field using specified name? Or you mean the text that is shown beside it? Can you please share a screenshot for our reference?