My checkboxes are always checked. I cannot create

2020-05-06 08:58发布

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.

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

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.

标签: itext
1条回答
姐就是有狂的资本
2楼-- · 2020-05-06 09:54

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

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

Regarding the setCheckType compare the other post.

查看更多
登录 后发表回答