In this core java tutorial we will learn How To Set Header and Footer in pdf in java using Itext Example using iText library - core java tutorial with program and examples.
We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.
Steps to Set Header and Footer in pdf in java using Itext >
- Create class HeaderAndFooterPdfPageEventHelper which extends com.itextpdf.text.pdf.PdfPageEventHelper class.
- Override onStartPage method of PdfPageEventHelper class which gets called when document.open() is called. You may set HEADER in this method. It also gets called whenever document.newPage() is called. (though in this example we haven’t used newPage() method)
- Override onEndPage method of PdfPageEventHelper class is called just before document is about to be closed. You may set FOOTER in this method.
- Create HeaderAndFooterPdfPageEventHelper instance in main method and set the page event using pdfWriter.setPageEvent(headerAndFooter);
How to create Bar Chart In Pdf in java - iText java tutorial
How to Modify - Add Text To Existing PDF in java - iText java tutorial example
Download jars required to execute program >
Program/Example How To Set Header and Footer in pdf in java using Itext library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
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;
/**
*
* How To Set Header and Footer in pdf Using Itext Example -
* Using iText library - core java tutorial
*
*/
class HeaderAndFooterPdfPageEventHelper extends PdfPageEventHelper {
public void onStartPage(PdfWriter pdfWriter, Document document) {
System.out.println("onStartPage() method > Writing header in file");
Rectangle rect = pdfWriter.getBoxSize("rectangle");
// TOP LEFT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP LEFT"), rect.getLeft(),
rect.getTop(), 0);
// TOP MEDIUM
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP MEDIUM"),
rect.getRight() / 2, rect.getTop(), 0);
// TOP RIGHT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("TOP RIGHT"), rect.getRight(),
rect.getTop(), 0);
}
public void onEndPage(PdfWriter pdfWriter, Document document) {
System.out.println("onEndPage() method > Writing footer in file");
Rectangle rect = pdfWriter.getBoxSize("rectangle");
// BOTTOM LEFT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("BOTTOM LEFT"),
rect.getLeft()+15, rect.getBottom(), 0);
// BOTTOM MEDIUM
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("BOTTOM MEDIUM"),
rect.getRight() / 2, rect.getBottom(), 0);
// BOTTOM RIGHT
ColumnText.showTextAligned(pdfWriter.getDirectContent(),
Element.ALIGN_CENTER, new Phrase("BOTTOM RIGHT"),
rect.getRight()-10, rect.getBottom(), 0);
}
}
public class HowToSet_HeaderFooter_InPdfUsingItextExample extends
PdfPageEventHelper {
public static void main(String[] args) throws DocumentException, IOException {
String pdfFilePath = "e:/Set Header and Footer in Pdf Using Itext Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
Rectangle rectangle = new Rectangle(30, 30, 550, 800);
pdfWriter.setBoxSize("rectangle", rectangle);
HeaderAndFooterPdfPageEventHelper headerAndFooter =
new HeaderAndFooterPdfPageEventHelper();
pdfWriter.setPageEvent(headerAndFooter);
document.open();
document.add(new Paragraph("This is Header and Footer in Pdf Using Itext Example"));
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
}
}
/* OUTPUT of above program/Example -
onStartPage() method > Writing header in file
onEndPage() method > Writing footer in file
onStartPage() method > Writing header in file
PDF created in >> e:/Set Header and Footer in Pdf Using Itext Example.pdf
*/
|
PDF formed after executing above java program will look like this >
Summary -
In this core java tutorial we learned How To Set Header and Footer in pdf in java using Itext Example using iText library - core java tutorial with program and examples.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
References >
RELATED LINKS>
iText library tutorial in java - How to Create and work with PDF files in java
Creating the Table and set the Column Width And Spacing In Pdf in java
Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example
Creating LIST in pdf in java - iText tutorial >
Create Greek List In pdf in java using iText - iText java tutorial
How to Create Roman List In pdf in java - iText java tutorial
Create ZapfDingbats List In pdf in java - iText java tutorial program
How to Create Ordered List In pdf in java using iText - iText java tutorial
Creating the UnOrdered List In pdf in java - iText java tutorial example
How to Create Lists And SubLists In Pdf in java - iText java tutorial
Creating PASSWORD PROTECTED PDF in java - iText tutorial >
How to Create Password Protected Pdf in java - iText Example
Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >
How to Set the Font Name, Size, Style and Colour In Pdf using itext in java
Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >
How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial
How To Set SuperScript And SubScript In Pdf in java - iText java tutorial
UnderLine Text In Pdf in java - iText java tutorial example
Strikethrough Text In Pdf in java - iText java tutorial program
HTML to PDF in java - iText tutorial >
Convert Html To Pdf in java using iText - iText java tutorial
Create new page, set page height and width in java - iText tutorial >
How To Create New Pages In Pdf Using Itext - iText java tutorial example
How to Find Pdf Page Height And Width in java - iText java tutorial program
Labels:
Core Java
iText Pdf tutorial