当用户将光标放在其中时,如何使用 disappear/be 可替换的文本填充 PDF 上的文本框?

How can I populate Textboxes on a PDF with text that will disappear/be replaceable when the user places the cursor in them?

使用 iTextSharp,我生成了一个如下所示的 PDF 文件:

虽然它仍然需要一些美化,但它进展得相当顺利,除了一件事:填充的文本框不可覆盖。您可以键入 over,是的,但是旧值仍保留在下方,如您所见。在 "Required Date" 字段中,我在“8/12/2015”上键入了“9”,但 8 仍然显示。电子邮件框也是如此,我在电子邮件地址(等)上输入 "bla"

这需要工作的方式是在网页上输入的值以编程方式输入到适当的框中(例如 "Reuired Date" 和 "Email" 的情况),但它们应该也可以删除,而不是与新文本共存。

这是我用来创建文本框的代码,以日期为例:

PdfPTable tblFirstRow = new PdfPTable(7);
tblFirstRow.WidthPercentage = 100;
tblFirstRow.SpacingBefore = 4f;
float[] FirstRowWidths = new float[] { 137f, 138f, 140f, 135f, 50f, 150f, 250f };
tblFirstRow.SetWidths(FirstRowWidths);
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;

Phrase phraseReqDate = new Phrase("Required Date: ", timesRoman9Font);
PdfPCell cellReqDate = GetCellForBorderlessTable(phraseReqDate, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellReqDate);

PdfPCell cellReqDateTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxReqDate"),
    Phrase = new Phrase(boxRequestDate.Text, timesRoman9Font)
};
tblFirstRow.AddCell(cellReqDateTextBox);

// For dynamically creating TextBoxes; from 
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;
        iTextSharp.text.pdf.TextField text = new iTextSharp.text.pdf.TextField(writer, rectangle, fieldname);
        PdfFormField field = text.GetTextField();
        writer.AddAnnotation(field);
    }
}

我还希望 "reminder" 文本(关于在什么地方输入什么)在用户以相同方式单击它们时显示为 "go away"。

我哪里做错了,或者我需要更改什么才能让这些文本框发挥应有的作用?

你应该删除这一行:

Phrase = new Phrase(boxRequestDate.Text, timesRoman9Font)

正如 mkl 在他的评论中所解释的那样,这一行会将文本 boxRequestDate 添加到页面中。该内容不是交互式的。它是页面内容流的一部分,而不是显示字段值的小部件注释的一部分。

相反,您需要调整 DynamicTextbox 活动。您已经有一个名为 fieldname 的成员变量。现在你应该添加一个额外的变量:

private string fieldname;
private string fieldvalue;

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

你应该像这样使用这个值:

iTextSharp.text.pdf.TextField text = new iTextSharp.text.pdf.TextField(writer, rectangle, fieldname);
text.Text = fieldvalue;

现在文本不会添加到页面中,但会存储在字段字典的 /V(值)键中,用于创建 /AP (外观)用于小部件注释。当你select那个小部件注释时,你可以改变那个字段的值并改变它。外观会相应变化。