【问题标题】:How can I create buttons to add to a PDF file using iTextSharp?如何使用 iTextSharp 创建按钮以添加到 PDF 文件?
【发布时间】:2015-06-23 06:33:10
【问题描述】:

我正在使用 iTextSharp 创建“标签”、文本框和复选框,但我尝试从该代码派生来创建按钮的尝试尚未成功。

这是我创建文本框的方式:

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

这是我创建复选框的方式:

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

这是我尝试创建按钮的方式

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

最后一个(按钮)代码有什么遗漏或错误?我认为我在 DynamicButton.CellLayout() 中对“字段”的分配一定是错误的,但应该是什么?

以下是“按钮”的外观(底部的细长线条,复选框及其相关文本/标签下方):

我怎样才能让那些纤细的“按钮”变得丰满/滋润?

【问题讨论】:

    标签: c# pdf-generation itextsharp pdf-form pdfptable


    【解决方案1】:

    您没有展示代码中最重要的部分:您没有展示如何创建定义按钮的单元格。如果您检查rect 的值(更具体地说是它的高度),您会发现您可能正在创建高度为0 的单元格。为什么高度为零?因为它们可能不包含任何东西。

    单元格是否必须包含任何内容?当然不是,但是如果你想避免零高度的行,你必须定义一个高度。这在我的书的Chapter 4 中进行了解释,例如在CellHeights 示例中。我为这个相当神秘的名字道歉,但这个例子是关于细胞的高度。

    你可以定义一个固定的高度:

    cell.FixedHeight = 72f;
    

    这是一个危险的属性:如果您添加的内容超出了固定高度(例如 72 个用户单位),该内容将会丢失。

    您还可以定义最小高度:

    cell.MinimumHeight = 36f;
    

    在这种情况下,高度肯定是 36 个用户单位(在这种情况下),但如果内容多于最小高度,则可能会更多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2016-01-29
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多