我正在使用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);
这里,水平对齐工作正常,但垂直对齐无效。
答案 0 :(得分: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);
答案 1 :(得分:0)
ALIGN_MIDDLE在iText代码中定义了整数值5。在你写ALIGN_MIDDLE时请注意提示“垂直元素的可能值”。这意味着如果您的元素处于垂直方向,那么它将在计算元素的中心时起作用。我的建议是用ALIGN_CENTER替换ALIGN_MIDDLE,这样你的代码就像:
cell.setVerticalAlignment(Element.ALIGN_CENTER);
答案 2 :(得分:0)
试试这个:
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_CENTER);