RadioButtonOptionField Crashes when trying to place in Tabel Cell

Hi Team,

I am trying to add RadioButtonOptionField to a tabel cell, But it is throwing the NullPointerException. I don’t want to mention Rectangle parameter because i need to add it to a table cell than fixing the position using rectangle.

Version: aspose.pdf-16.10.0.jar

Error:
at com.aspose.pdf.Annotation.m1(Unknown Source)
at com.aspose.pdf.WidgetAnnotation.m1(Unknown Source)
at com.aspose.pdf.Field.m1(Unknown Source)
at com.aspose.pdf.Annotation.m1(Unknown Source)
at com.aspose.pdf.RadioButtonField.add(Unknown Source)

Code:
Table table = new Table();
table.setMargin(new MarginInfo(20, 20,10, 0));
table.setColumnWidths(“50 50”);
pdfPageItem.getParagraphs().add(table);

RadioButtonField radio = new RadioButtonField(pdfPageItem);
RadioButtonOptionField opt1 = new RadioButtonOptionField(); – > Null exception here
opt1.setOptionName(“Test1”);
opt1.setName(“Test1”);
opt1.setCaption(new TextFragment(“Test1”));

Row row = table.getRows().add();
Cell cellYesField = row.getCells().add();
cellYesField.getParagraphs().add(opt1);

pdfDoc.getForm().add(radio);
pdfDoc.save(“Sample.pdf”);

Please let me know how i can add the options to a table cell rather than using fixed coordinates using rectangle.

Thanks,
Leena Robert

Hi Leena,

Thanks for using our API’s.

I have tested and modified your code a little bite to achieve your required result. Please check below sample code for your reference. I have attached the output PDF document also.

Java
Document doc = new Document();
Page page = doc.getPages().add();
Table table = new Table();
//table.setColumnWidths(“120 120 120”);
page.getParagraphs().add(table);
Row r1 = table.getRows().add();
Cell c1 = r1.getCells().add();
Cell c2 = r1.getCells().add();
RadioButtonField rf = new RadioButtonField(page);
rf.setPartialName(“radio”);
doc.getForm().add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
opt1.setOptionName(“Item1”);
opt2.setOptionName(“Item2”);
opt1.setWidth(15);
opt1.setHeight(15);
opt2.setWidth(15);
opt2.setHeight(15);
rf.add(opt1);
rf.add(opt2);
opt1.setCaption(new TextFragment(“Item1”));
opt2.setCaption(new TextFragment(“Item2”));
c1.getParagraphs().add(opt1);
c2.getParagraphs().add(opt2);
doc.save(“RadioButtonField.pdf”);

If you still face any issue, please feel free to contact us.

Best Regards,

Thank you Fahad, it really helped.

Hi Fahad,

Can you also help me to set the label of a checkbox
Code
CheckboxField chk1= new CheckboxField(pdfDoc);
chk1.setPartialName(“checkBox1”);
chk1.setWidth(15);
chk1.setHeight(15);
chk1.setContents(“Check 1”); -> Label is not getting set,

I tried setting the Content but still I am not able to attach a label to a checkbox as we did for radio button

RadioButtonOptionField opt1 = new RadioButtonOptionField();
opt1.setCaption(new TextFragment(“Item 1”)); --> This will assign the label


Also can you provide a similar example for ChoiceField as I am not able to find any good examples for ChoiceFiled in your GitHub code.

Thanks,
Leena Robert

Hello Leena,

Thanks for your feedback for the first query. It is good to know that suggest code worked for you.

If you need to add grouped Checkbox in PDF document, please go through our documentation link.

In order to set the label of a checkbox, unfortunately we don’t have this feature as of RadioButton now. I have logged PDFJAVA-36467 an enhancement request in our issue tracking system to set the caption of a checkbox field. You can use TextFragment and add it in a table cell, please check below sample code for your reference.

JAVA

Document doc = new Document();
Page page = doc.getPages().add();
Table table = new Table();
page.getParagraphs().add(table);
Row r1 = table.getRows().add();
Cell c1 = r1.getCells().add();
Cell c2 = r1.getCells().add();

CheckboxField chk1= new CheckboxField();
chk1.setPartialName(“checkBox1”);
chk1.setWidth(15);
chk1.setHeight(15);

c1.getParagraphs().add(chk1);
c2.getParagraphs().add(new TextFragment(“Checkbox Caption”));

doc.save(“CheckboxField.pdf”);

If you need further assistance, please feel free to contact us.

Best Regards,

Hi,

Thanks for the response.
For checkbox I am trying to give border, but the border doesn’t show up.
Also I don’t see any difference in the control when I set the readonly field to true as it still allow the user to check it. Is there a way I can disable the checkbox?
Same query of readonly is applicable to radiobutton also. RadioButton also remains active.

Row r2 = table.getRows().add();
Cell c1 = r2.getCells().add();
Cell c2 = r2.getCells().add();
c2.setAlignment(0);
Cell c3 = r2.getCells().add();
Cell c4 = r2.getCells().add();
c4.setAlignment(0);

CheckboxField chk1= new CheckboxField(pdfDoc);
chk1.setPartialName(“checkBox1”);
chk1.setWidth(15);
chk1.setHeight(15);
chk1.setContents(“Check 1”);

chk1.setBorder(new Border(chk1));
chk1.getBorder().setWidth(2);
chk1.getBorder().setStyle(BorderStyle.Solid);
chk1.getCharacteristics().setBorder(Color.BLACK);
chk1.getDefaultAppearance().setTextColor(Color.BLACK);
//chk1.setReadOnly(true);

CheckboxField chk2= new CheckboxField(pdfDoc);
chk2.setPartialName(“checkBox2”);
chk2.setWidth(15);
chk2.setHeight(15);
chk2.setContents(“Check 2”);

chk2.setBorder(new Border(chk2));
chk2.getBorder().setWidth(2);
chk2.getBorder().setStyle(BorderStyle.Solid);
chk2.getCharacteristics().setBorder(Color.BLACK);
chk2.getDefaultAppearance().setTextColor(Color.BLACK);
//chk2.setReadOnly(true);

// pdfDoc.getForm().add(chk1, 1);
// pdfDoc.getForm().add(chk2, 1);

c1.getParagraphs().add(chk1);
c2.getParagraphs().add(new TextFragment(“Check 1”));
c3.getParagraphs().add(chk2);
c4.getParagraphs().add(new TextFragment(“Check 2”));

Hello Leena,


For checkbox I am trying to give border, but the border doesn’t show up.
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}


I have tested the scenario and have managed to reproduce the problem that border is not showing up for the checkbox field. For the sake of correction, I have logged it as PDFJAVA-36471 in our issue tracking system.


Also I don't see any difference in the control when I set the readonly field to true as it still allow the user to check it. Is there a way I can disable the checkbox?


I have tested the scenario and have managed to reproduce the problem that readonly field to true is not working properly. For the sake of correction, I have logged another ticket as PDFJAVA-36472 in our issue tracking system.


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

We will further look into the details of these issues and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.


Best Regards,

Hi Team,

Please let me know when you will be getting the defect fixed.
I am depending on this defect for my work,

If you are able to give me a work around for these defects, it would be highly appreciable.

Thanks,
Leena Robert

Hi Leena,


Thanks for your inquiry. We have further investigated the issue related to readonly Checkbox. Please note that Adobe Acrobat uses transparent background and transparent border for not checked readonly Checkbox fields so the value will be visible only when checkbox is checked:

chk1.setChecked(true);

Some other PDF viewers show borders and gray background for readonly fields even if they were not checked.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

We are sorry that your other issue is still pending for resolution. Please note that issues are being resolved on first come first serve basis. There are other reported issues in the queue as well. We will update you on status through this thread once your issue is resolved. Please have patience and spare us a little time. We are sorry for the inconvenience.


Best Regards,