I'm able to create textboxes and a checkbox this way:
PdfPTable tblFirstRow = new PdfPTable(5);
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;
Chunk reqDate = new Chunk("Required Date: ", timesRoman9Font);
Paragraph parReqDate = new Paragraph();
parReqDate.Add(reqDate);
PdfPCell cellReqDate = new PdfPCell(parReqDate);
cellReqDate.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellReqDate);
PdfPCell cellReqDateTextBox = new PdfPCell()
{
CellEvent = new DynamicTextbox("textBoxReqDate"),
Phrase = new Phrase("4/14/2015"),
FixedHeight = 16
};
tblFirstRow.AddCell(cellReqDateTextBox);
Chunk paymentAmount = new Chunk("Payment Amount: ", timesRoman9Font);
Paragraph parPaymentAmount = new Paragraph();
parPaymentAmount.Add(paymentAmount);
PdfPCell cellPaymentAmount = new PdfPCell(parPaymentAmount);
cellPaymentAmount.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellPaymentAmount);
PdfPCell cellPaymentAmountTextBox = new PdfPCell()
{
CellEvent = new DynamicTextbox("textBoxPaymentAmount")
};
tblFirstRow.AddCell(cellPaymentAmountTextBox);
PdfPCell cell204Submitted = new PdfPCell()
{
CellEvent = new DynamicCheckbox("checkbox204Submitted", "204 Submitted or on file")
};
tblFirstRow.AddCell(cell204Submitted);
doc.Add(tblFirstRow);
These are the classes/events called to create the textboxes and checkbox:
public class DynamicTextbox : IPdfPCellEvent
{
private string fieldname;
public DynamicTextbox(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
float textboxheight = 12f;
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
TextField text = new TextField(writer, rect, fieldname);
PdfFormField field = text.GetTextField();
writer.AddAnnotation(field);
}
}
public class DynamicCheckbox : IPdfPCellEvent
{
private string fieldname;
private string cap;
public DynamicCheckbox(string name, String caption)
{
fieldname = name;
cap = caption;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
ckbx.CheckType = RadioCheckField.TYPE_CHECK;
ckbx.Text = cap;
PdfFormField field = ckbx.CheckField;
writer.AddAnnotation(field);
}
}
...but the checkbox's text is not displaying. The checkbox (outsized) occupies the last cell in the table row, but there is no (visible) accompanying text.
What's missing from my code?
Note: I tried to reduce the size of the checkbox by doing this:
Rectangle tangle = new Rectangle(20, 20);
RadioCheckField ckbx = new RadioCheckField(writer, tangle, fieldname, "Yes");
...but it failed epic[al]ly - now I can't even "find" the checkbox - clicking willy-nilly in column 5 conjures up no checkbox...