我正在尝试使用iText和java生成pdf,以下是使用的代码,但我无法将文本对齐在PdfPCell的中心,虽然我已经浏览了谷歌上的所有链接并尝试设置 setHorizontalAlignment(Element.ALIGN_CENTER); 但这对我不起作用。
带细胞的表格:
PdfPTable heading1=new PdfPTable(3);
try{
heading1.setWidths(new int[]{1,1,1});
heading1.setTotalWidth(527);
heading1.setLockedWidth(true);
heading1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
heading1.getDefaultCell().setFixedHeight(40);
/* PdfPCell heading1Text1 = new PdfPCell();
heading1Text1.setBorder(Rectangle.NO_BORDER);
heading1Text1.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 12)));
heading1Text1.setUseAscender(true);
heading1Text1.setHorizontalAlignment(Element.ALIGN_CENTER);
heading1.addCell(heading1Text1);*/
PdfPCell heading1Text2 = new PdfPCell();
heading1Text2.setColspan(3);
heading1Text2.setBorder(Rectangle.NO_BORDER);
heading1Text2.setHorizontalAlignment(Element.ALIGN_CENTER);
heading1Text2.addElement(new Phrase("RECEIPT", new Font(Font.FontFamily.HELVETICA, 12,Font.BOLD)));
heading1.addCell(heading1Text2);
/* PdfPCell heading1Text3 = new PdfPCell();
heading1Text3.setBorder(Rectangle.NO_BORDER);
heading1Text3.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 12)));
heading1.addCell(heading1Text3);*/
// write content
document.add(heading1);
}catch(DocumentException d){
throw new ExceptionConverter(d);
}