Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table Cell HorizontalAlignment is ignored/broken

Tags:

itext

itext7

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();
}
like image 259
Rui Lopes Avatar asked Jan 06 '23 15:01

Rui Lopes


2 Answers

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.

like image 96
Bruno Lowagie Avatar answered Jan 08 '23 05:01

Bruno Lowagie


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)
like image 22
Rui Lopes Avatar answered Jan 08 '23 03:01

Rui Lopes