奇怪的显示的单选按钮表 [英] Strange display of a table of radio buttons

查看:121
本文介绍了奇怪的显示的单选按钮表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Good OK Bad 

Item1 [button] [button] [button ]
Item2 [button] [button] [button]

每一行都是一个按钮组。



我的程序能够像上面这样的小表工作。但是,当行或列要保留多个页面时,显示很奇怪。这是我的程序:

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

table.setSpacingBefore(10);
table.setSpacingAfter(0);
table.setKeepTogether(false); //测试true不同显示错误

//左上角空单元
cell = new CustomPdfPCell();
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);

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

列表< PdfFormField> radioGroups = new ArrayList< PdfFormField>();

//每行
for(TableOfChoicesHeader r:rowHeaders){

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

//当前行行
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.setHorizo​​ntalAlignment(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);
}

以下是 table.setKeepTogether(false)时的屏幕截图)。显示有点不同但如果值 true 仍然很奇怪。



Page 1
< a href =https://i.stack.imgur.com/ZDyQd.jpg =nofollow noreferrer>



Page 2



更新



按照Samuel的方法,我可以看到第一页上显示的单选按钮。但是,第一页上的按钮显示在第二页上。请参阅屏幕截图。







更新2



我知道了通过在MyCellField中添加以下内容来工作:

  writer.addAnnotation(radiogroup); 

而不是

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

我无法解释原因。感谢Samuel和Bruno的帮助!

解决方案

再次查看您的代码,是否有任何理由为此添加注释radiobuttons一个接一个到底?
您是否尝试在添加细胞行时添加注释?这样的事情:

  for(TableOfChoicesHeader r:rowHeaders){

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

//当前行行
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.setHorizo​​ntalAlignment(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)

}

编辑:



正如Bruno所说,在多个页面上分发无线电组的示例是



我基本上把你的代码带到了上面的问题(有一些针对自定义数据类的存根实现),并使用 MyCellField PdfCellEvent扩展href =http://developers.itextpdf.com/examples/form-examples/create-fields-table#1178-radiogroupmultipage2.java =nofollow noreferrertitle =RadioGroupMultiPage2> RadioGroupMultiPage2

  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 ,价值);
PdfFormField field = radio.getRadioField();
writer.addAnnotation(field);
radiogroup.addKid(field);
} catch(IOException ex){
//什么都不做
} catch(DocumentException ex){
//什么都不做
}
}


I am creating a form, in which there is a table of buttons. The following shows the idea:

             Good         OK         Bad

Item1      [button]    [button]    [button]
Item2      [button]    [button]    [button]

Each row is a button group.

My program is able to work for a small table like the above. However, when rows or columns are more than one page to hold, the display is weird. This is my program:

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

Here are the screenshots when table.setKeepTogether(false). The display is a little different but still strange if the value is true.

Page 1

Page 2

Update

By following Samuel's approach, I am able to see radio buttons displayed on the first page. However, the buttons on the first page got displayed on the second page. See the screenshots.

Update 2

I got it working by adding the following in MyCellField :

writer.addAnnotation(radiogroup);  

instead of

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

I cannot explain why. Thank Samuel and Bruno for their help!

解决方案

Looking at your code again, is there any reason why you add the annotations for the radiobuttons all in one go at the end?. Have you tried adding the annotation whenever you've added your row of cells? Something like this:

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)

}

EDIT:

As Bruno said, the examples for distributing a radio-group over multiple pages are RadioGroupMultiPage1 and RadioGroupMultiPage2. The examples for distributing a radio group across a table is found here: Distributing radio buttons of a radio field across multiple PdfCells.

EDIT 2, Electric Bogaloo:

Wipped up a quick example based on your code and the examples Bruno pointed out:

I basicly took your code frm the question above(with some stub-implementations for your custom data-classes), and used the MyCellField PdfCellEvent extension found in RadioGroupMultiPage2:

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

这篇关于奇怪的显示的单选按钮表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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