【问题标题】:How to add a footer with multiple lines using iText 1.5.4?如何使用 iText 1.5.4 添加多行页脚?
【发布时间】:2023-03-26 11:05:01
【问题描述】:

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

基本上我想要实现的是这样的

【问题讨论】:

  • 您是否看到 OP 很少进入 SO 并且只有 3 个问题中的 1 个具有真实答案?您不能相信他,但也许可以相信已发布的答案。

标签: java itext


【解决方案1】:

我建议你按照这个例子:

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

您将能够添加任何您想要的页眉和页脚。我过去一直在使用它,并且能够在页脚中放置一个表格。

在他们的示例中注意 class HeaderFooter extends PdfPageEventHelperpublic void onEndPage(PdfWriter writer, Document document)

希望这会有所帮助!

【讨论】:

  • 我已经阅读了该链接中的示例,这就是为什么我在这里问的原因,因为示例太复杂并且没有真正回答我的问题,我一直在寻找更直接的东西来实现我随问题发布的图片。
  • 当然,这对于一页文档来说很复杂,但对于更多,这是要走的路。
【解决方案2】:

我使用下面的代码实现了与我在问题中发布的图像类似的布局。 我对 itext 不是很熟悉,所以如果有人有更好的方法来做到这一点,您的 cmets 和建议将不胜感激。 谢谢!

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();

}

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 2015-06-04
    • 2020-04-26
    • 2016-11-30
    • 2015-12-05
    • 2013-11-08
    • 2012-05-20
    • 2013-11-20
    相关资源
    最近更新 更多