我试图将文档中的表格垂直对齐到页面底部。
我已将表格的垂直对齐方式设置为BOTTOM,但这仅使单元格与表格本身的底部对齐。
如何使文档本身与底部垂直对齐?
谢谢
最佳答案
经过数天的搜索..我的解决方案是将我的表包装在具有1个单元格的外部表中。将单元格设置为页面高度减去两个页边距,然后将垂直对齐方式设置为底部。将您要底部对齐的所有内容添加到此表中。
完整示例,为简洁起见,省略了错误代码
public void run() {
try {
Document document = new Document(PageSize.LETTER, 10.0f, 10.0f, 36.0f, 36.0f);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
PdfPTable outerTable = new PdfPTable(1);
outerTable.setWidthPercentage(100.0f);
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(document.getPageSize().getHeight() - 36.0f - 36.0f);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
cell.addElement(createTable());
outerTable.addCell(cell);
document.add(outerTable);
document.newPage();
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public PdfPTable leftTable() {
PdfPTable table = new PdfPTable(1);
for (int i = 0; i < 50; i++) {
table.addCell("Cell: " + i);
}
return table;
}
public PdfPTable middleTable() {
PdfPTable table = new PdfPTable(1);
for (int i = 0; i < 10; i++) {
table.addCell("Cell: " + i);
}
return table;
}
public PdfPTable rightTable() {
PdfPTable table = new PdfPTable(1);
for (int i = 0; i < 100; i++) {
table.addCell("Cell: " + i);
}
return table;
}
public PdfPTable createTable() {
PdfPTable table = new PdfPTable(3);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
table.addCell(leftTable());
table.addCell(middleTable());
table.addCell(rightTable());
return table;
}