Itext 单选按钮表的奇怪显示

Itext 单选按钮表的奇怪显示,itext,Itext,我正在创建一个表单,其中有一个按钮表。以下是想法: Good OK Bad Item1 [button] [button] [button] Item2 [button] [button] [button] 每行是一个按钮组 我的程序能够为上面这样的小表工作。但是,当行或列包含多个页面时,显示会很奇怪。这是我的节目: PdfPTable table = new PdfPTable(colH

我正在创建一个表单,其中有一个按钮表。以下是想法:

             Good         OK         Bad

Item1      [button]    [button]    [button]
Item2      [button]    [button]    [button]
每行是一个按钮组

我的程序能够为上面这样的小表工作。但是,当行或列包含多个页面时,显示会很奇怪。这是我的节目:

 PdfPTable table = new PdfPTable(colHeaders.size() + 1  );
 table.setWidthPercentage(100);
 PdfPCell cell;

 table.setSpacingBefore(10);
 table.setSpacingAfter(0);
 table.setKeepTogether(false); //tested "true" different display error

 //upper-left corner empty cell
 cell = new CustomPdfPCell();
 cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
 table.addCell(cell);

 //header row
 for (TableOfChoicesHeader c: colHeaders) {
    String text = c.getText();
    cell = new PdfPCell(new Phrase(text));
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(cell);
 }

 List<PdfFormField> radioGroups= new ArrayList<PdfFormField>();

 //for each row
 for (TableOfChoicesHeader r: rowHeaders) {

    //row header
    String rowText = r.getText();
    cell = new PdfPCell(new Phrase(rowText));
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(cell);

 //for the current row row
 String fieldName = "question" + q.getId() +"_" + r.getId().toString();

 PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
 radiogroup.setFieldName(fieldName);

 radioGroups.add(radiogroup);

 for (TableOfChoicesHeader c: colHeaders) {     
     cell = new PdfPCell();
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     cell.setPadding(5);
     cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId()));
     table.addCell(cell);                   
   }

 }
 document.add(table);

 for (PdfFormField g: radioGroups) {
    writer.addAnnotation(g);
 }
而不是

PdfFormField field = radio.getRadioField();
writer.addAnnotation(field);

我无法解释原因。感谢塞缪尔和布鲁诺的帮助

再次查看您的代码,是否有任何理由在末尾一次性添加单选按钮的注释?。 您是否尝试过在添加单元格行时添加注释?大概是这样的:

for (TableOfChoicesHeader r: rowHeaders) {

    //row header
    String rowText = r.getText();
    cell = new PdfPCell(new Phrase(rowText));
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(cell);

    //for the current row row
    String fieldName = "question" + q.getId() +"_" + r.getId().toString();

    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
    radiogroup.setFieldName(fieldName);

    radioGroups.add(radiogroup);

    for (TableOfChoicesHeader c: colHeaders) {     
         cell = new PdfPCell();
         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cell.setPadding(5);
         cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId()));
         table.addCell(cell);                   
    }
    writer.addAnnotation(radiogroup)

}
编辑:

正如布鲁诺所说,在多个页面上分发广播组的示例有和。在表中分布无线组的示例如下:

编辑2,电动Bogaloo:

根据您的代码编写了一个快速示例,Bruno指出:

我基本上是从上面的问题开始考虑您的代码(使用一些用于自定义数据类的存根实现),并使用了
MyCellField
PdfCellEvent扩展,该扩展位于:


再次查看代码,是否有任何理由在末尾一次性添加单选按钮的注释?。 您是否尝试过在添加单元格行时添加注释?大概是这样的:

for (TableOfChoicesHeader r: rowHeaders) {

    //row header
    String rowText = r.getText();
    cell = new PdfPCell(new Phrase(rowText));
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(cell);

    //for the current row row
    String fieldName = "question" + q.getId() +"_" + r.getId().toString();

    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
    radiogroup.setFieldName(fieldName);

    radioGroups.add(radiogroup);

    for (TableOfChoicesHeader c: colHeaders) {     
         cell = new PdfPCell();
         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cell.setPadding(5);
         cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId()));
         table.addCell(cell);                   
    }
    writer.addAnnotation(radiogroup)

}
编辑:

正如布鲁诺所说,在多个页面上分发广播组的示例有和。在表中分布无线组的示例如下:

编辑2,电动Bogaloo:

根据您的代码编写了一个快速示例,Bruno指出:

我基本上是从上面的问题开始考虑您的代码(使用一些用于自定义数据类的存根实现),并使用了
MyCellField
PdfCellEvent扩展,该扩展位于:


塞缪尔,我在发帖子之前测试过了。如果在document.add(table)之前执行此操作,则根本不会显示任何单选按钮。谢谢你的想法!嗨,塞缪尔和好奇1,请看和。您会注意到,在不同页面上分发单选按钮时需要特别小心。@curious1,我用一个例子更新了我的答案。至于你做错了什么,我想可以从你的
CheckBoxCellEvent
实现和示例中的实现之间的差异中找到。这很奇怪,因为我的代码没有相同的问题,所有按钮都很好地限制在表中,我的第二页上没有任何溢出/重复。我看看能不能复制一下。我的直觉告诉我,由于一些剩余的试用代码,你在某处添加了重复的代码,但如果不检查你的最终代码,我无法确认。塞缪尔,非常感谢你的帮助!我对你的评论投了赞成票。由于你的解决方案激励我尝试不同的事情,而你的方案与我的方案非常接近,所以我选择了你的方案作为答案。干杯塞缪尔,我在发帖子之前测试过了。如果在document.add(table)之前执行此操作,则根本不会显示任何单选按钮。谢谢你的想法!嗨,塞缪尔和好奇1,请看和。您会注意到,在不同页面上分发单选按钮时需要特别小心。@curious1,我用一个例子更新了我的答案。至于你做错了什么,我想可以从你的
CheckBoxCellEvent
实现和示例中的实现之间的差异中找到。这很奇怪,因为我的代码没有相同的问题,所有按钮都很好地限制在表中,我的第二页上没有任何溢出/重复。我看看能不能复制一下。我的直觉告诉我,由于一些剩余的试用代码,你在某处添加了重复的代码,但如果不检查你的最终代码,我无法确认。塞缪尔,非常感谢你的帮助!我对你的评论投了赞成票。由于你的解决方案激励我尝试不同的事情,而你的方案与我的方案非常接近,所以我选择了你的方案作为答案。干杯
   public MyCellField(PdfWriter writer, PdfFormField radiogroup, String value) {
        this.writer = writer;
        this.radiogroup = radiogroup;
        this.value = value;
    }

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        try {
            RadioCheckField radio = new RadioCheckField(writer, position, null, value);
            PdfFormField field = radio.getRadioField();
            writer.addAnnotation(field);
            radiogroup.addKid(field);
        } catch (IOException ex) {
            // do nothing
        } catch (DocumentException ex) {
            // do nothing
        }
    }