0

Using iTextSharp, I've generated a PDF file that looks like this:

enter image description here

While it still needs some beautifying, it's coming along fairly well, except for one thing: The textboxes populated are not overwritable. You can type over them, yes, but the old values remain below, as you can see. In the "Required Date" field, I typed "9" over the "8/12/2015" and the 8 still shows through. The same goes for the Email box, where I typed "bla" over my email address (etc.)

The way this needs to work is that values entered on a web page are programmatically entered into the appropriate boxes (such as is the case here with "Reuired Date" and "Email"), but they should also be deletable, rather than coexisting with the new text.

Here is the code I use to create the textboxes, using Date as an example:

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 http://stackoverflow.com/questions/2675895/itextsharp-text-field-in-pdfpcell
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);
    }
}

I also want the "reminder" text (about what to enter where) to "go away" when the user clicks in them the same way.

What am I doing wrong, or what do I need to change to allow these Textboxes to function as they should?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Your code creates cells with both a phrase and a cell event (for form field generation). This makes the phrase be drawn in the regular page content. Regular page content won't go away when you start typing in the field. You might want create an initial field appearance with that phrase instead. – mkl Aug 13 '15 at 05:45

1 Answers1

1

You should remove this line:

Phrase = new Phrase(boxRequestDate.Text, timesRoman9Font)

As mkl explains in his comment, this line will add the text boxRequestDate to the page. That content is not interactive. It is part of the page's content stream, not of the widget annotation that shows the value of a field.

Instead you need to adapt your DynamicTextbox event. You already have a member variable named fieldname. Now you should add an extra variable:

private string fieldname;
private string fieldvalue;

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

You should use this value like this:

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

Now the text won't be added to the page, but it will be stored in the /V (value) key of the field dictionary and it will be used to create an /AP (appearance) for the widget annotation. When you select that widget annotation, you can change the value of that field and change it. The appearance will change accordingly.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165