2015-04-01 56 views
2

我想制作一个带有多行的PdfPTable。在每一行中,我希望在第一个单元格中具有一个单选按钮,并在第二个单元格中具有描述性文本。我希望所有的单选按钮都是同一个广播组的一部分。跨多个PdfPCell的iText RadioGroup/RadioButtons

我已经使用过PdfPCell.setCellEvent和我自己定制的cellEvents来在PDFPDable中呈现TextFields和Checkboxes。但是,我似乎无法弄清楚如何用单选按钮/收音机组来完成它。

iText可能吗?有没有人有一个例子?

回答

2

请看看CreateRadioInTable的例子。

在这个例子中,我们创建用于所述无线电基的PdfFormField,我们构建并添加表后添加:

PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true); 
radiogroup.setFieldName("Language"); 
PdfPTable table = new PdfPTable(2); 
// add cells 
document.add(table); 
writer.addAnnotation(radiogroup); 

当我们创建细胞单选按钮,我们添加一个事件,例如:

cell.setCellEvent(new MyCellField(radiogroup, "english")); 

事件看起来是这样的:

class MyCellField implements PdfPCellEvent { 
    protected PdfFormField radiogroup; 
    protected String value; 
    public MyCellField(PdfFormField radiogroup, String value) { 
     this.radiogroup = radiogroup; 
     this.value = value; 
    } 
    public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { 
     final PdfWriter writer = canvases[0].getPdfWriter(); 
     RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value); 
     try { 
      radiogroup.addKid(radio.getRadioField()); 
     } catch (final IOException ioe) { 
      throw new ExceptionConverter(ioe); 
     } catch (final DocumentException de) { 
      throw new ExceptionConverter(de); 
     } 
    } 
} 
+0

梦幻般的答案!非常感谢! – corestruct00 2015-04-01 17:31:51

1

采取这种远一点......

如果你嵌套在另一个表单选按钮(单选按钮组)的一个表,你就必须改变从布鲁诺的例子如下:

代替

document.add(table); 
writer.addAnnotation(radiogroup); 

使用(假设您创建了一个父表,并在名为parentCell该表中的PdfPCell)

parentCell.addElement(table); 
parentCell.setCellEvent(new RadioGroupCellEvent(radioGroup)); 

与父母细胞事件像这样

public class RadioGroupCellEvent implements PdfPCellEvent { 

    private PdfFormField radioGroup; 

    public RadioGroupCellEvent(PdfFormField radioGroup) { 
     this.radioGroup = radioGroup; 
    } 

    @Override 
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
     PdfWriter writer = canvases[0].getPdfWriter(); 
     writer.addAnnotation(radioGroup); 
    } 
} 
+1

是的,这可确保您的radiogroup被添加到正确的页面上。请注意,关于单选按钮的外观还有许多其他增强功能。我的代码只是一个概念证明;这是一个“裸体”的例子;-) – 2015-04-01 18:27:27