跨多个PdfPCell的iText RadioGroup/RadioButtons [英] iText RadioGroup/RadioButtons across multiple PdfPCells

查看:223
本文介绍了跨多个PdfPCell的iText RadioGroup/RadioButtons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有多行的PdfPTable.在每一行中,我想在第一个单元格中有一个单选按钮,在第二个单元格中有描述性文本.我希望所有单选按钮都属于同一单选组.

I'd like to make a PdfPTable with multiple rows in it. In each row I'd like to have a Radio button in the first cell and descriptive text in the second cell. I'd like all the radio buttons to be a part of the same radio group.

我过去曾经使用PdfPCell.setCellEvent和我自己的自定义cellEvent在PdfPTables中渲染TextField和Checkbox.但是,我似乎无法弄清楚如何使用单选按钮/单选按钮组.

I've used PdfPCell.setCellEvent and my own custom cellEvents in the past to render TextFields and Checkboxes in PdfPTables. However, I can't seem to figure out how to do it with Radio buttons/Radio groups.

使用iText可以吗?有人有例子吗?

Is this possible with iText? Does anyone have an example?

推荐答案

请查看 CreateRadioInTable 示例.

在此示例中,我们为单选组创建一个PdfFormField,并在构造并添加表格后将其添加:

In this example, we create a PdfFormField for the radio group and we add it after constructing and adding the table:

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

当我们为单选按钮创建单元格时,我们添加一个事件,例如:

When we create the cells for the radio buttons, we add an event, for instance:

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

这篇关于跨多个PdfPCell的iText RadioGroup/RadioButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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