使用iTextSharp的表中的RadioGroup

时间:2015-10-30 07:32:47

标签: c# .net pdf-generation itextsharp

我一直在使用iTextSharp处理可编辑PDF 的生成。

问题:

我想在表格的列中创建广播组字段。

我试过,我的代码是:

//Gender section starts here --->
//Table creation
PdfPTable genderFieldTable = new PdfPTable(1);
genderFieldTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
genderFieldTable.DefaultCell.FixedHeight = 100f;
genderFieldTable.TotalWidth = 400;
genderFieldTable.LockedWidth = true;    

//Creating the RadioButton group starts here ---->

PdfFormField _radioGroup = PdfFormField.CreateRadioButton(pdfWriter, true);
_radioGroup.FieldName = "Gender";

string[] genders = { "Male", "Female" };

RadioCheckField genderRadioCheckField = null;
PdfFormField radioGField;
for (int i = 0; i < genders.Length; i++)
{
    genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(200, 806, 170, 890), null, genders[i]);
    //,,40, 806 - i * 40, 60, 788 - i * 40
    genderRadioCheckField.BackgroundColor = BaseColor.LIGHT_GRAY;
    genderRadioCheckField.Rotation = 180;
    genderRadioCheckField.CheckType = RadioCheckField.TYPE_CIRCLE;
    radioGField = genderRadioCheckField.RadioField;
    ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(genders[i], new Font(Font.FontFamily.HELVETICA, 8)), 70, 790 - i * 40, 0);
    _radioGroup.AddKid(radioGField);
}

//pdfWriter.AddAnnotation(_radioGroup);
iTextSharp.text.pdf.events.FieldPositioningEvents genderEvents
    = new iTextSharp.text.pdf.events.FieldPositioningEvents(pdfWriter, genderRadioCheckField.RadioField);
PdfPCell genderFieldCell = new PdfPCell();
genderFieldCell.FixedHeight = 75f;

genderFieldCell.CellEvent = genderEvents;
genderFieldTable.AddCell(genderFieldCell);
formTable.AddCell(genderFieldTable);

我收到了男性和女性文字(不是无线电圆圈字段),但它位于某个地方,而不是在桌子内部。

请帮我把Radiogroup放在桌子里。

2 个答案:

答案 0 :(得分:0)

由于您设置了Rectangle坐标,单选按钮应位于可能位于页面外的文档上。

尝试:

genderRadioCheckField = new RadioCheckField(writer, new Rectangle(40, 806 - i * 40, 60, 788 - i * 40), null, genders[i]);

而不是

genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(200, 806, 170, 890), null, genders[i]);

如果您使用那些应该是您预期结果的值,则无需注释掉该行下的值。希望这会有所帮助。

答案 1 :(得分:0)

请查看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);
        }
    }
}