5

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();
}
Rui Lopes
  • 93
  • 1
  • 6
  • Hi, I encourage you to accept Bruno's answer to this question as I find it really thorough and it might be helpful to easier find an answer for other persons who encounter this question. – Alexey Subach Aug 20 '16 at 13:03

2 Answers2

9

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:

enter image description here

There's no problem with the alignment, neither vertical, horizontal, or text alignment.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
4

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)
Rui Lopes
  • 93
  • 1
  • 6