Alignment issue with multiline text form fields in

2019-07-13 02:35发布

问题:

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.

回答1:

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:

  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.



回答2:

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.