如何在Java中使用itext在pdf文档的最后一页底部创建表
问题描述:
我已经在Java中使用itext在pdf文档中创建了表格.在功能方面做得很好. 但是我需要将表格显示为文档的下方.
I have created table in pdf document using itext in java. Working fine in functionality wise. But I need to display table as a below of document.
这是我的代码
PdfPTable datatablebottom = new PdfPTable(8);
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(8);
cell.setBackgroundColor(BaseColor.GRAY);
cell.setBorderWidthTop(5.0f);
cell.setBorderColorTop(BaseColor.DARK_GRAY);
if(msgfb.equals("1")){
//document.add(new Paragraph(""));
cell.addElement(new Paragraph(""));
}else if(msgfb.equals("2")){
//document.add(new Paragraph("Thank you for your business"));
Paragraph pf = new Paragraph("Thank you for your business Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness",BT_NORMAL);
pf.setAlignment(Element.ALIGN_CENTER);
cell.addElement(pf);
}else{
//document.add(new Paragraph(msgfb));
Paragraph pf = new Paragraph(msgfb,BT_NORMAL);
pf.setAlignment(Element.ALIGN_CENTER);
cell.addElement(pf);
//cell.addElement(new Paragraph(msgfb,BT_NORMAL));
}
cell.setPaddingBottom(10.0f);
datatablebottom.addCell(cell);
datatablebottom.setTotalWidth(PageSize.A4.getWidth()-70);
datatablebottom.setLockedWidth(true);
document.add(datatablebottom);
答
您需要定义表格的绝对宽度:
You need to define the absolute width of your table:
datatable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));
然后您需要替换该行:
document.add(datatablebottom);
与此:
datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()), datatable.getTotalHeight() + document.bottom(document.bottomMargin()), writer.getDirectContent());
writeSelectedRows()
方法在绝对位置绘制表格.我们通过询问document
的左边缘(x值)并将表格的高度添加到文档的底部边缘(Y坐标)来计算该位置.我们绘制所有行(0到-1).
The writeSelectedRows()
method draws the table at an absolute position. We calculate that position by asking the document
for its left margin (the x value) and by adding the height of the table to the bottom margin of the document (the Y coordinate). We draw all rows (0 to -1).