Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm able to create textboxes and a checkbox this way:

C#
PdfPTable tblFirstRow = new PdfPTable(5);
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;

// "Required Date"
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
    // set borders, or other cell options here
};
tblFirstRow.AddCell(cellReqDateTextBox);

// "Payment Amount"
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);

// try a checkbox
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:

C#
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:

C#
Rectangle tangle = new Rectangle(20, 20);
//RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
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...
Posted
Updated 15-Apr-15 5:44am
v2
Comments
HKHerron 15-Apr-15 13:09pm    
Not sure, but don't you need to specify the size of the rectangle?
Like this:
RadioCheckField ckbx = new RadioCheckField(writer, new Rectangle(20, 20), fieldname, "Yes");
B. Clay Shannon 15-Apr-15 13:42pm    
I tried that, and the checkbox doesn't appear at all, then. I will probably just add an ancillary "label" (a collection of Chunks added to a Paragraph) aligned to the right of the checkbox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900