itext PDF:页脚仅在最后一页上生成

时间:2019-05-07 18:22:59

标签: java itext pdf-generation footer

我正在生成PDF中的1K行的列表,每页有50行。 我需要在每个页面中添加一个2行页脚。所以我添加了PdfPageEventHelper,但是它只在最后一页打印,只有一行。页码也没有打印。我尝试了来自itext5网站的示例。仍然不能

这是下面的代码。

    public byte[] exportToPDF(List<T> searchResult, String serviceName) throws IOException{

        Document document = new Document(PageSize.A4.rotate(), 25f, 25f, 25f, 50f);
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();)
        {
            PdfWriter writer = PdfWriter.getInstance(document, out);
            document.open();
            PdfPTable table = new PdfPTable(8);
            table.setWidthPercentage(100);
            printDataRows(searchResult, table);
            //Adding Footer
            document.add(table);
            HeaderFooter event = new HeaderFooter();
            writer.setPageEvent(event); 
            document.close();       
            return out.toByteArray();  
    }catch (Exception e)
    {
        e.printStackTrace();
        logger.error(PDF_GEN_ERROR + e);
    }
}

这是活动的代码

static class HeaderFooter extends PdfPageEventHelper
{
public HeaderFooter()
{
    super();
}



@Override
public void onEndPage(PdfWriter writer, Document document)
{

    PdfPTable table = new PdfPTable(3);
    try
    {
    Font regularFonts = FontFactory.getFont(FontFactory.HELVETICA);
    regularFonts.setSize(6);
    table.setTotalWidth(document.right() - document.left());
    table.setLockedWidth(true);
    table.getDefaultCell().setFixedHeight(10);
    table.getDefaultCell().setBorder(0);

    PdfPCell cv1 = new PdfPCell(new Phrase("Project Name", regularFonts));

    cv1.setFixedHeight(8);
    cv1.setBorder(0);
    cv1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(cv1);

    PdfPCell cv2 = new PdfPCell(new Phrase(String.format("Page %d of  ", writer.getPageNumber), regularFonts));

    cv2.setFixedHeight(8);
    cv2.setBorder(0);
    cv2.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cv2);

    PdfPCell cv3 = new PdfPCell(new Phrase("Product Group", regularFonts));

    cv3.setFixedHeight(8);
    cv3.setBorder(0);
    cv3.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(cv3);
    table.completeRow();

    PdfPCell cv21 = new PdfPCell(new Phrase("Printed on " + todaysDate(), regularFonts));

    cv21.setFixedHeight(8);
    cv21.setBorder(0);
    cv21.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(cv21);

    PdfPCell cv22 = new PdfPCell(new Phrase(""));
    cv22.setBorder(0);
    table.addCell(cv22);

    PdfPCell cv23 = new PdfPCell(new Phrase(new Phrase("Proprietary and Confidential", regularFonts)));

    cv23.setBorder(0);
    cv23.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(cv23);
    table.completeRow();
    PdfContentByte canvas = writer.getDirectContent();
    canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
    table.writeSelectedRows(0, -1, 34, 40, canvas);
    canvas.endMarkedContentSequence();
    }
    catch (Exception de)
    {
    throw new SystemException(PDF_GEN_ERROR, de);
    }

}
}

1 个答案:

答案 0 :(得分:0)

将表添加到文档后,设置事件处理程序:

PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
...
document.add(table);
HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event); 
document.close();

因此,当您设置事件处理程序时,除最后一页之外的所有页面都已被写入,您的事件处理程序将仅在最后一页被调用。

要让页面事件处理程序检索所有页面的事件,请尽早进行设置,例如

PdfWriter writer = PdfWriter.getInstance(document, out);
HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event); 
document.open();
...
document.add(table);
document.close();

这样,您的页面事件处理程序将为每个页面调用。