Programmatically Create PDF Header And Footer In Java

In this blog, we will learn how to add headers and footers when creating a new PDF document in Java applications.

Introduction 

 
When we operate PDF documents, at the PDF header and footer area we can add some additional information to make the PDF more attractive and readable. In this post, I'll give you an example of how to add a header and footer when creating a new PDF document in Java applications.
 

Before Starting

 
Get two jars which should be referenced to your Java project - Spire.Pdf jar and Spire.common jar. Free Spire.PDF 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. 
  1. import java.awt.*;  
  2. import java.awt.geom.Dimension2D;  
  3. import com.spire.pdf.*;  
  4. import com.spire.pdf.automaticfields.PdfAutomaticField;  
  5. import com.spire.pdf.automaticfields.PdfCompositeField;  
  6. import com.spire.pdf.automaticfields.PdfPageCountField;  
  7. import com.spire.pdf.automaticfields.PdfPageNumberField;  
  8. import com.spire.pdf.graphics.*;  
  9. public class PDFHeaderFooter {  
  10.  public static void main(String[] args) throws Exception {  
  11.   PdfDocument doc = new PdfDocument();  
  12.   PdfMargins margin = new PdfMargins(60604040);  
  13.   addHeaderAndFooter(doc, PdfPageSize.A4, margin);  
  14.   PdfPageBase page1 = doc.getPages().add();  
  15.   PdfPageBase page2 = doc.getPages().add();  
  16.   PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 14));  
  17.   String text1 = "Demo of Spire.PDF";  
  18.   String text2 = "How to add header and footer to PDF in JAVA";  
  19.   page1.getCanvas().drawString(text1, font, PdfBrushes.getBlack(), 00);  
  20.   page2.getCanvas().drawString(text2, font, PdfBrushes.getBlack(), 00);  
  21.   doc.saveToFile("output/headerFooter.pdf");  
  22.   doc.close();  
  23.  }  
  24.  static void addHeaderAndFooter(PdfDocument doc, Dimension2D pageSize, PdfMargins margin) {  
  25.   PdfPageTemplateElement header = new PdfPageTemplateElement(margin.getLeft(), pageSize.getHeight());  
  26.   doc.getTemplate().setLeft(header);  
  27.   PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.getWidth(), margin.getTop());  
  28.   topSpace.setForeground(true);  
  29.   doc.getTemplate().setTop(topSpace);  
  30.   PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 12));  
  31.   PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);  
  32.   String label = "E-iceblue Co.,Ltd";  
  33.   Dimension2D dimension2D = new Dimension();  
  34.   dimension2D.setSize(font.measureString(label, format));  
  35.   float y = topSpace.getHeight() - font.getHeight() - 1;  
  36.   PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 0.75 f);  
  37.   topSpace.getGraphics().setTransparency(0.5 f);  
  38.   topSpace.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);  
  39.   y = y - 1 - (float) dimension2D.getHeight();  
  40.   topSpace.getGraphics().drawString(label, font, PdfBrushes.getBlack(), margin.getLeft(), y, format);  
  41.   PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.getRight(), pageSize.getHeight());  
  42.   doc.getTemplate().setRight(rightSpace);  
  43.   PdfPageTemplateElement footer = new PdfPageTemplateElement(pageSize.getWidth(), margin.getBottom());  
  44.   footer.setForeground(true);  
  45.   doc.getTemplate().setBottom(footer);  
  46.   y = font.getHeight() + 1;  
  47.   footer.getGraphics().setTransparency(0.5 f);  
  48.   footer.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);  
  49.   y = y + 1;  
  50.   PdfPageNumberField pageNumber = new PdfPageNumberField();  
  51.   PdfPageCountField pageCount = new PdfPageCountField();  
  52.   PdfCompositeField pageNumberLabel = new PdfCompositeField();  
  53.   pageNumberLabel.setAutomaticFields(new PdfAutomaticField[] {  
  54.    pageNumber,  
  55.    pageCount  
  56.   });  
  57.   pageNumberLabel.setBrush(PdfBrushes.getBlack());  
  58.   pageNumberLabel.setFont(font);  
  59.   format = new PdfStringFormat(PdfTextAlignment.Right);  
  60.   pageNumberLabel.setStringFormat(format);  
  61.   pageNumberLabel.setText("page {0} of {1}");  
  62.   pageNumberLabel.setBounds(footer.getBounds());  
  63.   pageNumberLabel.draw(footer.getGraphics(), pageSize.getWidth() - margin.getLeft(), y);  
  64.  }  
  65. }   
Here is an effective screenshot after adding the header and footer to the new PDF document in the Java application.
 
Programmatically Create PDF Header And Footer In Java