I'm using iText 7.0.0 (the Java flavor) and it seems the table cell HorizontalAlignment is ignored because neither CENTER nor RIGHT works. Can you reproduce this?
see the pdf screenshoot
and the code to reproduce:
private static void brokenTableCellHorizontalAlignmentPdf(OutputStream output) throws IOException {
PdfWriter writer = new PdfWriter(output);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] {15f, 16f, 4f}).setWidthPercent(100);
for (int y = 1; y <= 3; ++y) {
for (int x = 1; x <= 3; ++x) {
table.addCell(
new Cell()
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.add(new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)));
}
}
document.add(table);
document.close();
}
Please take a look at the CellAlignment example:
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(new float[]{2, 1, 1});
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.setTextAlignment(TextAlignment.CENTER);
table.addCell(new Cell(1, 3).add("Cell with colspan 3"));
table.addCell(new Cell(2, 1).add("Cell with rowspan 2")
.setTextAlignment(TextAlignment.RIGHT));
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
Cell cell = new Cell()
.add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT))
.add(new Paragraph("Center"))
.add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT));
table.addCell(cell);
cell = new Cell().add("Middle")
.setVerticalAlignment(VerticalAlignment.MIDDLE);
table.addCell(cell);
cell = new Cell().add("Bottom")
.setVerticalAlignment(VerticalAlignment.BOTTOM);
table.addCell(cell);
document.add(table);
document.close();
}
The resulting PDF when you run this example looks like this:
There's no problem with the alignment, neither vertical, horizontal, or text alignment.
Oh nevermind! After looking at another answer (which pointed to http://gitlab.itextsupport.com/itext7/samples/blob/develop/publications/highlevel/src/main/java/com/itextpdf/highlevel/chapter05/C05E03_CellAlignment.java) we should now use setTextAlignment
. As in:
new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)
.setTextAlignment(TextAlignment.CENTER)
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