0

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

我四处搜索并想出了将复选框放在表格中的方法。这是我创建一些表格单元格的方法。

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

}

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

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)作品看起来像它应该的那样。谢谢。

4

1 回答 1

0

尽管您已重复发布,但我会给您一些提示,代码有什么问题:

  1. 由于你想添加几个复选框,你必须在循环内创建你的复选框(但是之后添加注释,这有点奇怪恕我直言)
  2. 为什么要添加部分名称?
  3. 您设置了一个无效的空选项(又名导出值)
  4. [可选] 无需(不必要地)再次重新创建矩形。

好的,这是可以正常工作的代码:

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比较其他帖子。

于 2013-08-06T10:25:17.620 回答