简体   繁体   中英

Horizontal alignment not working in itextpdf layout cell

I'm currently working on a table and want all the values in each cell to be centered. My current code is this:

    cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
    cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
    cell.add(new Paragraph(text).setRotationAngle(hasRotation? rotation : 0));
    cell.setBorder(Border.NO_BORDER);
    return cell;

But my problem is, that the horizontal alignment doesn't work. It centers the text on the vertical length of the cell, but it's bound to the left side of the cell, even though I specified the horizontal alignment to be centered. You can see what I mean in the image below. Any help would be apprechiated

在此处输入图像描述

For anyone interested. The problem was, that you can't set the rotation and set the text to be centered. I replaced the code with this, which seems to work for me:

    cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
    Paragraph p = new Paragraph(text);
    p.setFontSize(fontSize);
    p.setTextAlignment(TextAlignment.CENTER);
    if (hasRotation) {
        p.setRotationAngle(rotation);
        p.setPaddingTop(factor * (cellWidth / 2) - fontSize / 1.5f);
    }
    cell.setBorder(Border.NO_BORDER);
    cell.add(p);
    return cell;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM