I am using Itext to create a pdf and I cannot get the checkbox to uncheck. Here is my code:
RadioCheckField bt = new RadioCheckField(writer, new Rectangle(300, 300, 400, 400),
"check1", "Yes");
bt.setCheckType(RadioCheckField.TYPE_CHECK);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
bt.setBorderColor(BaseColor.BLACK);
bt.setBackgroundColor(BaseColor.WHITE);
bt.setChecked(false);
PdfFormField ck = bt.getCheckField();
writer.addAnnotation(ck);
You can see that the bt.setChecked(false)
is in the code, but the checkbox is still checked. I looked at the docs and it seems to me that it is supposed to work this way. What do I not understand?
First this:
- You've posted the same question on Nabble which is not a site endorsed by iText. See http://lowagie.com/nabble for more info.
- As you were not subscribed to the official mailing-list, I had to manually approve your question. Now I see that you posted the same question here. Cross-posting is usually not appreciated in a community.
As for your question, I've answered it here: http://article.gmane.org/gmane.comp.java.lib.itext.general/65407
Bottom line: I made a Short, Self Contained, Correct (Compilable), Example based on your code and I executed it. I couldn't reproduce the problem you reported. Maybe there is no problem. Maybe there was a problem in a previous version of iText that has now been fixed.
I also read that you shipped your code with the text color set to white. I don't understand: that doesn't make sense! Your PDF will have an interactive field, but people will never be able to see whether or not it's checked...
If you don't care, if all you wanted is to show a checkbox, then using an interactive field is overkill. You should have used a check box character, for instance from the ZapfDingbats font.
If you're using the AGPL version of iText, please show me the URL where I can find your code (as you know, the AGPL requires code using AGPL software to be distributed as AGPL too).
Try the following way, for me it is working:
public void addRadioGroup() throws Exception{
if(!this.doc.isOpen()){
this.doc.open();
}
PdfFormField radioGroup = PdfFormField.createRadioButton(this.writer, false);
radioGroup.setFieldName("numbers");
for(int i=0;i<3;i++){
Rectangle rect = new Rectangle(130+(40*i), 430, 160+(40*i), 455);
this.addRadioButtonKid(radioGroup, rect,String.valueOf(i));
}
this.writer.addAnnotation(radioGroup);
}
private void addRadioButtonKid(PdfFormField radio, Rectangle rect, String onValue) throws Exception{
RadioCheckField bt = new RadioCheckField(this.writer, rect, null, onValue);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
bt.setBorderColor(Color.BLACK);
bt.setBackgroundColor(Color.WHITE);
bt.setCheckType(RadioCheckField.TYPE_CROSS);
bt.setChecked(false);
PdfFormField ck = bt.getCheckField();
ck.setPlaceInPage(1);
radio.addKid(ck);
}
The only problem I had was that the default "check style" wasn't changed. A user reported this problem back in 2011 on the mailinglist in 2011. If you need another style patch iText for yourself or use the workaround described by Mark.
Update: After 2 years they seem to have fixed the problem in the latest iText version 5.4.3 (cp. the change of Michaël Demey)
try using a checkbox instead of a radiobutton since you only have one