I am using itextSharp 4.1.6 to add AcroFields to an existing PDF and in a second step, fill these fields with content. When I open these documents in Adobe Reader, I can see the content. When I try to print the document, all filled fields are empty.
This happens with both, editable and flattened PDFs. 'GenerateAppearances' is set to True. Due to the current licence concept I cannot upgrade to another version. My code:
var dlg = new OpenFileDialog
{
FileName = "",
Filter = @"pdf|*.pdf"
};
if (dlg.ShowDialog() != DialogResult.OK) return;
// first step: create field
var outputpath1 = Path.Combine(Path.GetTempPath(), string.Format("{0}.pdf", Guid.NewGuid().ToString("N")));
var pdfReader1 = new PdfReader(dlg.FileName);
var output1 = new FileStream(outputpath1, FileMode.Create, FileAccess.Write);
var stamper1 = new PdfStamper(pdfReader1, output1);
InsertTextfield(stamper1, 1, 20, 500, 150, 100, "test");
stamper1.Close();
pdfReader1.Close();
// second step: insert some text
var outputpath2 = Path.Combine(Path.GetTempPath(), string.Format("{0}.pdf", Guid.NewGuid().ToString("N")));
var pdfReader2 = new PdfReader(outputpath1);
var output2 = new FileStream(outputpath2, FileMode.Create, FileAccess.Write);
var stamper2 = new PdfStamper(pdfReader2, output2);
stamper2.AcroFields.GenerateAppearances = true;
var formData = stamper2.AcroFields;
formData.SetField("test", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ");
stamper2.Close();
pdfReader2.Close();
Process.Start(outputpath2);
And the InsertTextField
:
private static void InsertTextfield(PdfStamper stamper, int page, float x, float y, float width, float height, string fieldname)
{
var field = PdfFormField.CreateTextField(stamper.Writer, true, false, 500);
field.SetWidget(new Rectangle(x, y, x + width, y + height), PdfAnnotation.HIGHLIGHT_INVERT);
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.FieldName = fieldname;
stamper.AddAnnotation(field, page);
}
For the document, just create a new word document, add some text and save as PDF. Choose this document to start with.
So, how can I get my filed contents to show up in prints?
EDIT: Added code