3

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 : enter image description here

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

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
Steve
  • 2,988
  • 2
  • 30
  • 47
  • 1
    *the problem is that I was using a font called 'Cambria'* - some fonts contain information making a program believe that its glyphs are much larger than they actually are. Maybe that is the problem here. – mkl May 14 '15 at 11:19
  • 1
    [In this question](http://stackoverflow.com/a/16584687/1729265) there also was a height issue with Cambria. – mkl May 14 '15 at 11:34

2 Answers2

5

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:

enter image description here

There are three possible reasons why your code behaves differently:

  1. Your form is different. There's something very specific about your text field. If so, please share your form for inspection. [Update: as mentioned in the comments, there was indeed something special about the text field; updating the field solved the problem.]
  2. You are adding too many newlines. Maybe "21 High Street," is already followed by a newline. Maybe there's something odd about the newline sequence on your system. Note that this is PDF: it is OS independent, just using /n is sufficient.
  3. Maybe you're using an unofficial version of iText. It is very frustrating for us (the iText Group), but some people found it necessary to create forks that contain code that simply doesn't behave the way the official iText behaves. Make sure that you're using a genuine copy of iText.

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks. I will have a look. The stamper.FormFlattening was set to false temporarily so I could actually see what was being put in the field (hence the inconsistency in code). – Steve May 14 '15 at 10:33
  • Hi Bruno, I have worked out what the problem was. 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. – Steve May 14 '15 at 11:11
  • So my first guess (option 1. Your form is different) was the correct one. Thanks for the feedback! – Bruno Lowagie May 14 '15 at 13:52
0

I found that if you make the text box on the PDF and the text box on the asp.net form the same font it fixes the format. No bold in asp.net text box.

Arraylist
  • 307
  • 3
  • 13
  • This has nothing to do with ASP .NET text boxes. The form is a PDF form which has values being dynamically generated. – Steve Mar 06 '17 at 16:30