我的复选框总是被选中。我无法取消选中它们 [英] My checkboxes are always checked. I cannot create them unchecked

查看:134
本文介绍了我的复选框总是被选中。我无法取消选中它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张用表创建的表单。在表格的单元格中,我有复选框。其中一些复选框需要检查,而其他复选框则不需要。

I have a form that I have created with a table. In side the cells of the table I have checkboxes. Some of these checkboxes need to be checked and others do not.

我用Google搜索并想出了将复选框放入表格的方法。这是我创建一些表格单元格的方法。

I googled around and came up with way to put checkboxes in the table. Here is my method that is creating a few of the table cells.

private void createFourColumnBody(String[] rowLabels, PdfPTable table) throws DocumentException {
    PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer);
    for (String label : rowLabels) {
        PdfPCell cell = table.getDefaultCell();
        cell = new PdfPCell(new Paragraph(label));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);

        cell = new PdfPCell(table.getDefaultCell());
        cell.setCellEvent(new CellField(writer, checkboxGroupField, true));
        table.addCell(cell);

        cell = new PdfPCell(table.getDefaultCell());
        cell.setCellEvent(new CellField(writer, checkboxGroupField, false));
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("                    "));
        table.addCell(cell);
    }

    getDocument().add(table);
    writer.addAnnotation(checkboxGroupField);

}

这是调用创建复选框的类。

This is the class that is called to create the checkboxes.

protected class CellField implements PdfPCellEvent {
    private PdfFormField parent;

    private String partialFieldName;
    private PdfWriter writer;
    private boolean checked;

    public CellField(PdfWriter writer, PdfFormField parent, boolean checked) {
        this.writer = writer;
        this.parent = parent;
        this.checked = checked;
    }

    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
        try {
            createCheckboxField(rect);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private void createCheckboxField(Rectangle rect) throws IOException, DocumentException {
        RadioCheckField rf = new RadioCheckField(writer, new Rectangle(rect.getLeft(2), rect.getBottom(2),
                rect.getRight(2), rect.getTop(2)), partialFieldName, "");
        rf.setChecked(checked);
        rf.setBorderColor(GrayColor.GRAYBLACK);
        rf.setBackgroundColor(GrayColor.GRAYWHITE);
        rf.setCheckType(RadioCheckField.TYPE_CHECK);

        parent.addKid(rf.getCheckField());
    }
}

你可以在第一种方法中看到我拥有选中布尔值,第一个复选框标记为true,第二个复选框标记为false,但它始终创建一个选中了复选框的pdf。我试过删除复选标记,只是绘制一个矩形,但它一直无济于事。需要做什么才能使 rf.setChecked(false)像它应该的那样工作。谢谢。

You can see in the first method that I have the checked boolean marked as true for the first checkbox and false for the second checkbox, but it always creates a pdf with the checkbox checked. I have tried removing the checkmark and just drawing a rectangle, but it has been to no avail. What needs to happen to make the rf.setChecked(false) work like it seems that it should. Thanks.

推荐答案

虽然你有双贴我会给你一些提示代码有什么问题:

Although you have double posted I will give you some hints what's wrong with the code:


  1. 由于你想要添加几个复选框,你必须在循环中创建复选框(但是之后添加注释,这有点奇怪imho)

  2. 为什么要添加部分名称?

  3. 您设置了一个无效的空选项(也就是导出值)

  4. [可选]无需(不必要地)重新创建矩形。

  1. Since you want to add several checkboxes you have to create your checkboxes inside the loop (however add the annotions afterwards which is a bit weird imho)
  2. Why did you add a partial name?
  3. You set an empty option (aka export value) which is invalid
  4. [optional] no need to (unnecessarily) recreated the rectangle again.

好的,这里的代码工作正常:

Ok here's the code which works fine:

public void createFourColumnBody(String[] rowLabels, PdfPTable table) throws DocumentException {
    this.doc.open();

    ArrayList<PdfFormField> list = new ArrayList<PdfFormField>();

    for (int i=0;i<rowLabels.length;i++) {

        //PdfFormField checkboxGroupField = PdfFormField.createRadioButton(this.writer,false);
        PdfFormField checkbox1 = PdfFormField.createCheckBox(this.writer);
        //create a unique name 
        checkbox1.setFieldName("checkbox-"+i);

        PdfFormField checkbox2 = PdfFormField.createCheckBox(this.writer);
        checkbox2.setFieldName("checkbox+"+i);
        //use for autofill
        //checkbox2.setFieldName("checkbox-"+i);

        PdfPCell cell = table.getDefaultCell();
        cell = new PdfPCell(new Paragraph(rowLabels[i]));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);

        cell = new PdfPCell(table.getDefaultCell());
        cell.setCellEvent(new CellField(this.writer, checkbox1, true,"1"));
        table.addCell(cell);

        cell = new PdfPCell(table.getDefaultCell());
        cell.setCellEvent(new CellField(this.writer, checkbox2, false,"1"));
        //use for autofill
        //cell.setCellEvent(new CellField(this.writer, checkbox1, true,"1"));
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("                    "));
        table.addCell(cell);

        list.add(checkbox1);
        list.add(checkbox2);
    }
    this.doc.add(table);

    for(PdfFormField field: list){
        this.writer.addAnnotation(field);
    }
}







    protected class CellField implements PdfPCellEvent {
    private PdfFormField parent;

    private PdfWriter writer;
    private boolean checked;
    private String onValue;

    public CellField(PdfWriter writer, PdfFormField parent, boolean checked, String onValue) {
        this.writer = writer;
        this.parent = parent;
        this.checked = checked;
        this.onValue = onValue;
    }

    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
        try {
            this.createCheckboxField(rect);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void createCheckboxField(Rectangle rect) throws IOException, DocumentException {
        RadioCheckField bt = new RadioCheckField(this.writer, rect, null, this.onValue);
        bt.setCheckType(RadioCheckField.TYPE_CROSS);
        bt.setChecked(this.checked);
        this.parent.addKid(bt.getCheckField());
    }
}

关于 setCheckType 比较另一篇文章。

这篇关于我的复选框总是被选中。我无法取消选中它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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