使用java sevlets和itext将页码添加到pdf

时间:2015-12-10 09:58:00

标签: java itext

我有这个代码 我在前端和servlet使用html并使用itext库

            PdfWriter writer =  PdfWriter.getInstance(doc, new FileOutputStream("/home/sam/Desktop/java22.pdf"));
            doc.open(); 
            writer.setPageEvent(PdfPageEventHelper);

           doc.addHeader("ss", "ss");
           doc.add(new Paragraph("Executive Summary"));
            doc.add( Chunk.NEWLINE );
            htmlWorker.parse(new StringReader(e1));
            doc.add( Chunk.NEWLINE );
            doc.add( Chunk.NEWLINE );

            doc.newPage();



            doc.add(new Paragraph("Project Overview"));
            doc.add( Chunk.NEWLINE );
            htmlWorker.parse(new StringReader(e2));

            doc.add( Chunk.NEWLINE );
            doc.add( Chunk.NEWLINE );
            doc.newPage();

}

必须在页脚中添加页面否 请帮帮我

1 个答案:

答案 0 :(得分:1)

PageEventHelper工具方法 onStartPage() onEndPage和pageCounter中。然后访问directContent以在底部位置添加文本。

int pageCount = 0;

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte imp = writer.getDirectContent();
    imp.beginText();

    //Play around here and adjust to values that fit nicely on your page
    imp.setTextMatrix(65f, 30f);

    imp.showText("Page " + (++pageCount));
    //End Text
    imp.endText();
}