为什么某些文本框不显示在我的 PDF 页面 (iTextSharp) 上?

Why do certain TextBoxes not display on my PDF page (iTextSharp)?

我在 PDF 表单上有几个文本框。大多数显示良好,其中包含适当的数据,例如 "Required Date" 文本框。其他的在没有传递数据时显示正常(例如"Payment Amount")。但是,底部的那些根本不显示。例如,查看表单的底部部分,从 "Requester / Payee Signature" 向下到该 ("Authorization") 部分的底部 - 只有水平线出现在这些标签下方,而不是应该在那里的文本框:

他们使用的代码几乎相同:

// "Request Date" is an example of those which displays fine:
PdfPCell cellReqDateTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxReqDate", boxRequestDate.Text)
};
tblFirstRow.AddCell(cellReqDateTextBox);
. . .
doc.Add(tblFirstRow);

// Requester / Payee Signature" stands for all those who foul up:
PdfPCell cellTextBoxRequesterPayeeSignature = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxRequesterPayeeSignature", "Enter signature here")
};
tblSection6_Row2.AddCell(cellTextBoxRequesterPayeeSignature);
. . .
doc.Add(tblSection6_Row2);

他们都这样称呼:

public class DynamicTextbox : IPdfPCellEvent
{
    private string fieldname;
    private string fieldvalue;

    public DynamicTextbox(string name, string value)
    {
        fieldname = name;
        fieldvalue = value;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;
        iTextSharp.text.pdf.TextField text = new iTextSharp.text.pdf.TextField(writer, rectangle, fieldname);
        text.Text = fieldvalue;
        PdfFormField field = text.GetTextField();
        writer.AddAnnotation(field);
    }
}

我能看到他们如何调用 "DynamicTextbox()" 的唯一区别是显示的那些将文本框的文本值作为第二个参数传递给 DynamicTextbox(),而那些不工作的传递原始字符串 - 为什么这很重要?

标签下面的"horizontal lines"是文本框吗?如果是,为什么他们的身高这么矮?

所有其他文本字段属于包含其他内容的行中的一个单元格。其他内容也决定了它们所属的单元格的高度,因此也决定了行的高度。

看起来好像由单行组成的文本字段属于包含没有内容的单元格的行。添加了这些单元格,但它们的高度为零。执行cell事件时,position参数是一个零高度的矩形,所以在这种事件中添加的字段只有一行。

为避免这种情况,您可以定义最小高度或固定高度。最小高度是指如果添加更多内容,高度可以获得比您定义的值更大的值。固定高度表示不符合您定义的高度的内容将不会显示。

使用:

cellTextBoxRequesterPayeeSignature.FixedHeight = 20f;

或:

cellTextBoxRequesterPayeeSignature.MinimumHeight = 20f;

将值 20f 调整为最适合您的应用程序上下文的值。计量单位为用户单位。默认值是 72 个用户单位等于 1 英寸。