JavaSheriff
JavaSheriff

Reputation: 7665

Vertical text in table header?

How to make single header text (header of table) flow vertically instead of horizontal?

I tried adding

setVerticalAlignment(VerticalAlignment.TOP)

I don't see it making any difference.

targetTable.addHeaderCell(new Paragraph("VerticalAlignment").setFont(myFont).setFontColor(ColorConstants.BLACK)).setVerticalAlignment(VerticalAlignment.TOP));

see second and third column header text alignment

Upvotes: 0

Views: 1297

Answers (1)

mkl
mkl

Reputation: 95918

As already mentioned in a comment, this is not a matter of vertical alignment (well, not primarily, we'll use the vertical alignment to vertically adjust the text in the outer header cells) but instead of rotated text.

You can create a table like in your image like this:

ISplitCharacters noSplit = (text, glyphPos) -> false;

Table table = new Table(4);

table.addHeaderCell(new Cell().add(new Paragraph("First Col Header"))
                              .setVerticalAlignment(VerticalAlignment.BOTTOM));
table.addHeaderCell(new Paragraph("Second Column Header")
                              .setRotationAngle(Math.PI / 2).setSplitCharacters(noSplit));
table.addHeaderCell(new Cell().add(new Paragraph("Third Column Header").setRotationAngle(Math.PI / 2)
                              .setSplitCharacters(noSplit)).setVerticalAlignment(VerticalAlignment.BOTTOM));
table.addHeaderCell(new Cell().add(new Paragraph("Fourth Column Header"))
                              .setVerticalAlignment(VerticalAlignment.BOTTOM));

table.addCell("Row 1 Description");
table.addCell("12");
table.addCell("15");
table.addCell("27");

table.addCell("Row 2 Description");
table.addCell("25");
table.addCell("12");
table.addCell("37");

table.addCell("Sum");
table.addCell("37");
table.addCell("27");
table.addCell("64");

doc.add(table);

(CreateTableWithRotatedHeader test testCreateTableForUser648026)

To get the rotated text we apply setRotationAngle(Math.PI / 2) to the paragraphs in question.

To prevent the rotated text to be split across multiple lines, we discourage splitting applying setSplitCharacters(noSplit).

To have the column texts to be at the bottom of the header cells, not at the (default) top, we apply setVerticalAlignment(VerticalAlignment.BOTTOM) to the cells.

The result:

Screen shot

Upvotes: 2

Related Questions