使用com.lowagie.text进行PDF单元格垂直对齐

Anu*_*rma 9 java pdf itext

我正在使用com.lowagie.text在我的代码中创建PDF.一切正常,除了我试图垂直对齐我的细胞内容.我希望单元格文本位于单元格高度的中间.

这是我的代码

PdfPCell cell = new PdfPCell(new Phrase(value, fontValueNew));
cell.setBorder(o);
cell.setBackgroundColor(new Color(233,232,232));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
Run Code Online (Sandbox Code Playgroud)

这里,水平对齐工作正常,但垂直对齐无效.

epo*_*och 0

我不太确定为什么,但这对我有用(垂直居中对齐):

String headingLabel = "Test";

Paragraph heading = new Paragraph(headingLabel,
        new Font(helvetica, 28, Font.NORMAL, new BaseColor(0, 0, 0)));

Float textWidth = ColumnText.getWidth(heading);
Float maxAllowed = 630f;

while (maxAllowed < textWidth) {
    fontSize -= 2;
    heading = new Paragraph(headingLabel,
        new Font(helvetica, fontSize, Font.NORMAL, new BaseColor(0, 0, 0)));
    textWidth = ColumnText.getWidth(heading);
}

heading.setAlignment(Element.ALIGN_CENTER);

PdfPCell titleCell = new PdfPCell();
titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
titleCell.setVerticalAlignment(Element.ALIGN_TOP);
titleCell.addElement(heading);
titleCell.setFixedHeight(65f);
headerTable.addCell(titleCell);
Run Code Online (Sandbox Code Playgroud)