Using iTextSharp, I've generated a PDF file that looks like this:
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?