'itext7 paragraph's vertical alignment: puzzling behaviour

I have this simple code for testing vertical alignment in paragraphs:

Paragraph p = new Paragraph();
p.setBackgroundColor(new DeviceRgb(189, 239, 73));
p.setBorder(new SolidBorder(0.2f));
p.setVerticalAlignment(VerticalAlignment.MIDDLE);
p.add("first line - vertical alignment MIDDLE");
p.add("\n second line");
p.add("\n 3rd line");
document.add(p);

p = new Paragraph();
p.setBackgroundColor(new DeviceRgb(249, 149, 183));
p.setBorder(new SolidBorder(0.2f));
p.setVerticalAlignment(VerticalAlignment.TOP);
p.add("first line - vertical alignment TOP");
p.add("\n second line");
p.add("\n 3rd line");
document.add(p);

p = new Paragraph();
p.setBackgroundColor(new DeviceRgb(149, 189, 253));
p.setBorder(new SolidBorder(0.2f));
p.setVerticalAlignment(VerticalAlignment.BOTTOM);
p.add("first line - vertical alignment BOTTOM");
p.add("\n second line");
p.add("\n 3rd line");
document.add(p);

The result is this: threee paragraphs with different alignment

It seems that only the first Text child is vertically aligned in every Paragraph: if it's the standard behaviour, it's somewhat confusing.

However if I use a Div and add a Paragraph to it and transfer all properties and child elements to the Div, all works fine. Why?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source