iText文档垂直对齐

7

我想要将文档中的表格垂直对齐到页面底部。

我已经将表格的垂直对齐方式设置为BOTTOM,但这只是使单元格对齐到表格底部。

如何使整个文档垂直对齐到底部呢?

谢谢。

1个回答

13

经过多天的搜索,我的解决方案是将表格放在一个只有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;
}

为什么最后会出现一个空白页面? - Amit K Kushwaha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接