如何使用iTextSharp创建添加到PDF文件的按钮? [英] How can I create buttons to add to a PDF file using iTextSharp?

查看:159
本文介绍了如何使用iTextSharp创建添加到PDF文件的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp创建标签"以及文本框和复选框,但是我尝试从该代码派生出创建按钮的尝试尚未成功.

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

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

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()中对字段"的赋值一定是错误的,但是应该是什么呢?

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):

我该如何充实/补水那些细小的纽扣"?

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

推荐答案

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

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.

一个单元格是否必须包含任何东西?当然不是,但是如果要避免高度为零的行,则必须定义一个高度.这在我的书的第4章中进行了解释,例如在 CellHeights 示例.我为这个比较神秘的名称表示歉意,但是这个示例是关于单元格的高度的.

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;

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

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;

在这种情况下,高度肯定是36个用户单位(在这种情况下),但是如果内容超出最小高度的容纳范围,则可能会更高.

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.

这篇关于如何使用iTextSharp创建添加到PDF文件的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆