Home  >  iText

Add Header and Footer in PDF Using iText in Java

By Arvind Rai, February 08, 2015
This page will provide the tutorial for how to add header and footer in every page of PDF using iText in java. iText provides PdfPageEventHelper class that has different event methods that is called while writing PDF. To add header and footer we need to create a class extending PdfPageEventHelper and override onStartPage and onEndPage methods. Register this class with PdfWriter. Find the complete explanation as below.

Create an Event Class Extending PdfPageEventHelper

PdfPageEventHelper class has different event methods for PDF. It implements PdfPageEvent interface. Some event methods of PdfPageEventHelper are as below

onOpenDocument
onStartPage
onEndPage
onCloseDocument
onParagraph
onParagraphEnd

To add header and footer, we need to override onStartPage and onEndPage methods. While defining these methods we do not need to use Document.add() method. To get the location for header and footer, we need to create a rectangle and get location by using Rectangle.getLeft, Rectangle.getRight, Rectangle.getTop and Rectangle.getBottom methods. We can use ColumnText to format text in column wise. For every page creation these events will be hit and perform the action as defined.
HeaderFooterPageEvent.java
package com.concretepage;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
public class HeaderFooterPageEvent extends PdfPageEventHelper {
    public void onStartPage(PdfWriter writer,Document document) {
    	Rectangle rect = writer.getBoxSize("art");
        ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new Phrase("Top Left"), rect.getLeft(), rect.getTop(), 0);
        ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new Phrase("Top Right"), rect.getRight(), rect.getTop(), 0);
    }
    public void onEndPage(PdfWriter writer,Document document) {
    	Rectangle rect = writer.getBoxSize("art");
        ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new Phrase("Bottom Left"), rect.getLeft(), rect.getBottom(), 0);
        ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new Phrase("Bottom Right"), rect.getRight(), rect.getBottom(), 0);
    }
} 



Add Header and Footer to PDF

Use event class to add header and footer. Using PdfWriter.setPageEvent , we can register our event class. Create a rectangle and set it to PdfWriter using PdfWriter.setBoxSize() method. In the example we are creating two pages. The event will automatically be called to add header and footer.
CreateHeaderFooter.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
public class CreateHeaderFooter  {
    public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HeaderFooter.pdf"));
        Rectangle rect = new Rectangle(30, 30, 550, 800);
        writer.setBoxSize("art", rect);
        HeaderFooterPageEvent event = new HeaderFooterPageEvent();
        writer.setPageEvent(event);
        document.open();
        document.add(new Paragraph("This is Page One"));
        document.newPage();
        document.add(new Paragraph("This is Page two"));
        document.close();
	System.out.println("Done");
    }
} 
Run the example and we will get a PDF of two page with header and footer text on each page.
Add Header and Footer in PDF Using iText in Java
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us