在iText中将PDFPTable添加到页面底部

时间:2012-10-09 09:09:41

标签: java itext

我正在尝试添加一个包含所有版权文本,页码等的页脚。但我找不到任何支持PdfPTable

的方法

对于短语,有如下代码:

ColumnText.showTextAligned(writer.getDirectContent(),
        Element.ALIGN_CENTER, new Phrase(
            String.format("%d", document.getPageNumber())),
            (document.getPageSize().getLeft() + document.getPageSize().getRight())/2,
            document.getPageSize().getBottom() + 18, 0);

3 个答案:

答案 0 :(得分:8)

PdfPTable类有一个方法writeSelectedRows(),可用于在绝对位置添加(选择列和行)。

示例:

答案 1 :(得分:7)

布鲁诺发布的例子是一个很好的指针,这里有一个没有幻数的例子:

    private void writeFooterTable(PdfWriter writer, Document document, PdfPTable table) {
        final int FIRST_ROW = 0;
        final int LAST_ROW = -1;
        //Table must have absolute width set.
        if(table.getTotalWidth()==0)
            table.setTotalWidth((document.right()-document.left())*table.getWidthPercentage()/100f);
        table.writeSelectedRows(FIRST_ROW, LAST_ROW, document.left(), document.bottom()+table.getTotalHeight(),writer.getDirectContent());
    }

这将在底部的文档边距内写入PdfPTable,与底部的任何文本重叠。如果您希望在保证金中写表,请使用:document.bottom()代替document.bottom()+table.getTotalHeight()


Header/Footer Example

作为相关说明,如果您关注此链接上的示例,那么" art"框似乎不是必需的,幻数36,54,559,788对应于:

document.left(), document.bottom(), document.right(), document.top()

答案 2 :(得分:0)

要实现自定义页脚,您需要实现PdfPageEventHelper