0

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

Alexander
  • 4,153
  • 1
  • 24
  • 37
Anja
  • 11
  • 2
  • 2
    1. Share your code 2. Share your documents 3. Try to reproduce the issue in the most recent release, 5.5.9. – Amedee Van Gasse May 19 '16 at 15:02
  • *This happens with both, editable and flattened PDFs* - if that also happens with flattened documents, something is very fishy, unless you use optional content groups which are switched off while printing. – mkl May 19 '16 at 15:10
  • Thank you for sharing your code. Now, try again with iTextSharp `5.5.9`. – Amedee Van Gasse May 23 '16 at 12:19
  • @ Amedee Van Gasse: Same result when using 5.5.9 – Anja May 30 '16 at 12:58

1 Answers1

0

You should create your TextField via the TextField constructor.
Works for me in iTextSharp 4.1.6

private static void InsertTextfield(PdfStamper stamper, int page, float x, float y, float width, float height, string fieldname)
{
    var field = new TextField(stamper.Writer, new Rectangle(x, y, x + width, y + height), fieldname);
    field.Options = TextField.MULTILINE;
    stamper.AddAnnotation(field.GetTextField(), page);
}
fuchs777
  • 993
  • 1
  • 14
  • 30