How can I create buttons to add to a PDF file usin

2019-08-21 10:32发布

I'm creating "labels" and textboxes and checkboxes using iTextSharp, but my attempts to derive from that code to create buttons has not yet been successful.

Here is how I'm creating textBoxes:

PdfPCell cellRequesterNameTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxRequesterName")
};
tblFirstRow.AddCell(cellRequesterNameTextBox);

. . .

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 = 11f;
        Rectangle rect = rectangle;
        rect.Bottom = rect.Top - textboxheight;

        TextField text = new TextField(writer, rect, fieldname);

        PdfFormField field = text.GetTextField();

        writer.AddAnnotation(field);
    }
}

And here is how I'm creating checkboxes:

PdfPCell cell204Submitted = new PdfPCell()
{
    CellEvent = new DynamicCheckbox("checkbox204Submitted")
};
tblLastRow.AddCell(cell204Submitted);
. . .

public class DynamicCheckbox : IPdfPCellEvent
{
    private string fieldname;

    public DynamicCheckbox(string name)
    {
        fieldname = name;
    }

    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.BackgroundColor = new BaseColor(255, 197, 0);
        ckbx.FontSize = 6;
        ckbx.TextColor = BaseColor.WHITE;
        PdfFormField field = ckbx.CheckField;
        writer.AddAnnotation(field);
    }
}

And here is how I'm attempting to create buttons:

PdfPCell cellButton1 = new PdfPCell()
{
    CellEvent = new DynamicButton("button1")
};
tblButtonRow.AddCell(cellButton1);
. . .

public class DynamicButton : IPdfPCellEvent
{
    private string fieldname;

    public DynamicButton(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;

        float buttonheight = 24f;
        Rectangle rect = rectangle;
        PushbuttonField btn = new PushbuttonField(writer, rect, fieldname);

        PdfFormField field = btn.Field; // <= what should this be?

        writer.AddAnnotation(field);
    }
}

What's missing or wrong in the last (button) code? I'm thinking my assignment to "field" in DynamicButton.CellLayout() must be wrong, but what should it be?

Here is how the "buttons" look (the long skinny lines at the bottom, beneath the checkbox and its associated text/label):

enter image description here

How can I plump up/hydrate those twiggified "buttons"?

1条回答
Rolldiameter
2楼-- · 2019-08-21 11:29

You aren't showing the most important part of the code: you aren't showing how you create the cells for which you define the buttons. If you would check the value of rect (more specifically its height), you'll discover that you are probably creating cells with a height that is 0. Why is the height zero? Because they probably don't contain anything.

Is it necessary for a cell to contain anything? Of course not, but if you want to avoid a row with zero height, you have to define a height. This is explained in Chapter 4 of my book, for instance in the CellHeights example. I apologize for the rather cryptic name, but this example is about the height of cells.

You can define a fixed height:

cell.FixedHeight = 72f;

This is a dangerous property: if you add content that doesn't fit inside the fixed height (e.g. 72 user units), that content will be lost.

You can also define a minimum height:

cell.MinimumHeight = 36f;

In this case, the height will certainly be 36 user units (in this case), but it could be more if there is more content than fits in the minimum height.

查看更多
登录 后发表回答