How to make a footer in generating a pdf file using an itextpdf

12,268

For implementing Header and footer you need to implement a HeaderFooter class that extends
PdfPageEventHelper class of iText API. Then override the onEndPage() to set header and footer. In this example I am settingname in header and 'page mumber` in footer.

In pdf creation side code you need to use HeaderAndFooter class like this:

    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, "C:\sample.pdf");
    //set page event to PdfWriter instance that you use to prepare pdf
    writer.setPageEvent(new HeaderAndFooter(name));
    .... //Add your content to documne here and close the document at last

    /*
     * HeaderAndFooter class
     */
    public class HeaderAndFooter extends PdfPageEventHelper {

    private String name = "";


    protected Phrase footer;
    protected Phrase header;

    /*
     * Font for header and footer part.
     */
    private static Font headerFont = new Font(Font.COURIER, 9,
            Font.NORMAL,Color.blue);

    private static Font footerFont = new Font(Font.TIMES_ROMAN, 9,
            Font.BOLD,Color.blue);


    /*
     * constructor
     */
    public HeaderAndFooter(String name) {
        super();

        this.name = name;


        header = new Phrase("***** Header *****");
        footer = new Phrase("**** Footer ****");
    }


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

        PdfContentByte cb = writer.getDirectContent();

        //header content
        String headerContent = "Name: " +name;

        //header content
        String footerContent = headerContent;
        /*
         * Header
         */
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(headerContent,headerFont), 
                document.leftMargin() - 1, document.top() + 30, 0);

        /*
         * Foooter
         */
        ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format(" %d ", 
                writer.getPageNumber()),footerFont), 
                document.right() - 2 , document.bottom() - 20, 0);

    }

}

Hope it helps. I had used this in one of the allication.

Share:
12,268
Weddy
Author by

Weddy

System.out.println("Weddy"); printf("is"); Console.WriteLine("my"); <html><body>name</body></html> document.write('and'); console.log('I am'); print('glad'); <?php echo 'to meet' ?> MsgBox "you."

Updated on July 12, 2022

Comments

  • Weddy
    Weddy almost 2 years

    I have been searching the net on how to make or set a footer using itextpdf in java. and up until now i havent found anything on how to do it. I have seen some articles on how to use and set a header. but not footers. here's a sample code

    Document document = new Document(PageSize.LETTER);
    
    Paragraph here = new Paragraph();
    Paragraph there = new Paragraph();
    
    Font Font1 = new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD);
    
    here.add(new Paragraph("sample here", Font1));
    there.add(new Paragraph("sample there", Font1));
    //footer here 
    
    document.add(here);
    document.add(there);
    document.add(footer);
    
  • Weddy
    Weddy about 11 years
    i figured out another way but this helped alot. hope this helps other users as well :D
  • dsi
    dsi almost 11 years
    thanks for this answer. I just need one addition as below: In footer, i have 3 lines and each in different font size . How can i add these three lines with different font in footer. How can i estimate font height and according to that set its vertical value - document.bottom()-XXXX ?