I have written an application to programatically fill out a form in a PDF template from a database and save the result to disk. Everything is working correctly apart from any multi-line text fields which are not rendering as expected. They should be top, left aligned with no gaps between lines.
The result I get is here :

However, when I click into the form field using any PDF reader, the field corrects itself to what I would expect. See :

The code I am using is pretty boiler plate stuff.
PdfStamper stamper = new PdfStamper(reader, ms, '\0', false);
AcroFields form = stamper.AcroFields;
List<DocumentField> fields = GetData(id);
foreach (DocumentField field in fields)
{
form.SetField(field.FieldName, field.Value);
}
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
I am using System.Environment.NewLine to add the carraige returns. Does anyone know what might be causing this behaviour and the solution to make the top left aligned without the large gaps. Thanks.
Update with solution
I removed the field and re-added it and it behaved as expected. What actually seems to be the problem is that I was using a font called 'Cambria' which if I set the field back to using that font, the behaviour returned.
Please take a look at the MultiLineField that was written to test your allegation. In this example, we create a form with the simplest possible multi-line text field:
Rectangle rect = new Rectangle(36, 720, 144, 806);
TextField tf = new TextField(writer, rect, "text");
tf.setOptions(TextField.MULTILINE);
writer.addAnnotation(tf.getTextField());
Then we fill and flatten this form like this:
public void createPdf(String filename) throws DocumentException, IOException {
PdfReader reader = new PdfReader(createForm());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
AcroFields form = stamper.getAcroFields();
form.setField("text", "A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street");
stamper.setFormFlattening(true);
stamper.close();
}
Take a close look at the String:
"A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street"
I introduce some newline characters (\n) and even a carriage return/newline sequence (\r\n). These introduce a single new line. Only when I introduce more than one newline character (\n\n), an extra gap is introduced:

There are three possible reasons why your code behaves differently:
/n is sufficient.In any case: the screen shots you shared in your example do not match your code. In your code, you have:
stamper.FormFlattening = true;
This is inconsistent with your screen shots that show interactivity. This is only possible if you have:
stamper.FormFlattening = false;
Or if this line isn't present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With