始终选中Itext复选框 [英] Itext checkbox is always checked

查看:1027
本文介绍了始终选中Itext复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Itext创建一个pdf,我无法取消选中该复选框。这是我的代码:

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);

你可以看到 bt.setChecked(false)在代码中,但仍然选中了复选框。我查看了文档,在我看来它应该以这种方式工作。我什么不明白?

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?

推荐答案

尝试以下方式,对我而言,它正在运作:

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);
}

我遇到的唯一问题是默认的检查风格不是改变。用户在2011年的邮件列表中报告了此问题在2011年。如果您需要自己的另一个样式补丁iText或使用Mark描述的解决方法。

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.

更新:2之后多年来,他们似乎已经解决了最新的 iText版本5.4.3 中的问题(cp.MichaëlDemey的变化)

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)

这篇关于始终选中Itext复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆