Add Header and Footer to PDF in Java

In my previous article, I’ve introduced how to add header and footer to a Word document. This article will share how to add header and footer when creating new PDF document by using Free Spire. PDF for Java.

Spire.PDF for Java has a class named PdfPageTemplateElement, which represents a page template element that can be used as header, footer, watermark or stamp. The template can contain text, image as well as dynamic fields like PdfPageCountField, PdfPageNumberField, etc. We use text string for the header and dynamic fields for the footer in the following example.

Import JAR Dependency

● Download the Free Spire.PDF for Java and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.


Sample Code

import java.awt.*;
import java.awt.geom.Dimension2D;
import com.spire.pdf.*;
import com.spire.pdf.automaticfields.PdfAutomaticField;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;

public class PDFHeaderFooter {
public static void main(String[] args) throws Exception {

//create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Set margins
PdfMargins margin = new PdfMargins(60,60,40,40);

//Call the method addHeaderAndFooter() to add header and footer
(doc, PdfPageSize.A4, margin);

//Add two pages to the PDF document and draw string to it.
PdfPageBase page1 = doc.getPages().add();
PdfPageBase page2 = doc.getPages().add();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 14));
String text1 = "This is a test file";
String text2 = "How to add header and footer to PDF Using JAVA";
page1.getCanvas().drawString(text1, font, PdfBrushes.getBlack(),0,0);
page2.getCanvas().drawString(text2, font, PdfBrushes.getBlack(),0,0);

//Save the document

static void addHeaderAndFooter(PdfDocument doc, Dimension2D pageSize, PdfMargins margin) {

PdfPageTemplateElement header = new PdfPageTemplateElement(margin.getLeft(), pageSize.getHeight());

PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.getWidth(), margin.getTop());

//Draw header label
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12));

PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String label = "Brief Introduction";
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(font.measureString(label, format));
float y = topSpace.getHeight() - font.getHeight() - 1;
PdfPen pen = new PdfPen(new PdfRGBColor(, 0.75f);
topSpace.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y - 1 - (float) dimension2D.getHeight();
topSpace.getGraphics().drawString(label, font, PdfBrushes.getBlack(), margin.getLeft(), y, format);

PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.getRight(), pageSize.getHeight());

//Draw dynamic fields as footer
PdfPageTemplateElement footer = new PdfPageTemplateElement(pageSize.getWidth(), margin.getBottom());

y = font.getHeight() + 1;
footer.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.setAutomaticFields(new PdfAutomaticField[]{pageNumber, pageCount});
format = new PdfStringFormat(PdfTextAlignment.Right);
pageNumberLabel.setText("page {0} of {1}");
pageNumberLabel.draw(footer.getGraphics(), - margin.getLeft(), y);


Sharing Java Code