如何使用iText 1.5.4添加多行的页脚?

时间:2013-03-06 05:54:46

标签: java itext

我的问题是如何使用iText 1.5.4添加多行的页脚 我知道我的问题类似于one,但直到现在这个问题都没有得到解答。此外,我不需要解决方案在页脚中使用表。

基本上我想要实现的是这样的 footer with multiple lines

2 个答案:

答案 0 :(得分:2)

我建议您按照这个例子:

http://itextpdf.com/examples/iia.php?id=103

您可以添加任何内容作为页眉和页脚。我过去一直在使用它,并且能够在页脚中放置一个表格。

请注意其示例中的class HeaderFooter extends PdfPageEventHelperpublic void onEndPage(PdfWriter writer, Document document)

希望这有帮助!

答案 1 :(得分:0)

我使用下面的代码实现了我想要的布局,类似于我在问题中发布的图像。 我不熟悉itext所以如果有人有更好的方法这样做你的意见和建议将非常感谢。 谢谢!

public static void main(String[] args) throws DocumentException, IOException{
    Document document = new Document(PageSize.A4, 50, 50, 70, 70);

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("C:/pdfwithfooter.pdf"));
        document.open();

        document.add(new Paragraph(
                "Hello Body of the PDF"));

        /** Format Font **/
        Font fontFooter = new Font(FontFactory.getFont(
                FontFactory.HELVETICA, 6));

        /**
         * Format Table Cell Borders
         */
        // cell.disableBorderSide(Rectangle.BOX);
        Rectangle page = document.getPageSize();
        PdfPTable foot = new PdfPTable(4);
        PdfPCell footCell = new PdfPCell(new Phrase(
                "OUR DOCUMENT'S LEGEND", FontFactory.getFont(
                        FontFactory.HELVETICA, 6, Font.BOLDITALIC)));
        footCell.setColspan(2);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase(""));
        footCell.setColspan(2);
        foot.addCell(footCell);

        // 1st row
        footCell = new PdfPCell(new Phrase("COX - COX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("EQ - Equipment", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("OUT-XYZ - Out XXXXXXXXXXXXX",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase(
                "ADJ-XX - Adjustment on XXXXXX XXXX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        // 2nd row
        footCell = new PdfPCell(new Phrase("EB - Electric Bills",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("T-BB - Transfer to Big Boat",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("T-XXXX - Transfer to XXXX",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("GX - Get XXXXXX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        // 3rd row
        footCell = new PdfPCell(new Phrase(
                "EXCEPTIONS - Invalid X Y VALUES", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("R XX - Removal of XX",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase("T-X - Transfer of XXXX",
                fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase(
                "T-LX - Transfer of Leased XXXX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        // 4th row
        footCell = new PdfPCell(new Phrase("LX - Leased XXXX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase(
                "T-TX - Transfer to TREE XXXXXXX", fontFooter));
        footCell.setPaddingLeft(15);
        foot.addCell(footCell);
        footCell = new PdfPCell(new Phrase(
                "Adj-XXX - Adjustment Some Value",
                fontFooter));
        footCell.setPaddingLeft(15);
        footCell.setColspan(2);
        foot.addCell(footCell);

        foot.setTotalWidth(page.width() - document.leftMargin()
                - document.rightMargin());
        foot.writeSelectedRows(0, -1, document.leftMargin(),
                document.bottomMargin(), writer.getDirectContent());

        document.close();

}