简体   繁体   中英

Add page number to pdf using java sevlets and itext

I have this code i am using html in front end and servlets and using itext library

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

}

have to add page no in footer please do help me

In your PageEventHelper implement method onStartPage() onEndPage and a pageCounter. Then access directContent to add text at bottom position.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM