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.

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.     
  10. public class PDFHeaderFooter {    
  11.     public static void main(String[] args) throws Exception {    
  12.     
  13.         //create a PdfDocument object    
  14.         PdfDocument doc = new PdfDocument();    
  15.     
  16.         //Set margins    
  17.         PdfMargins margin = new PdfMargins(60,60,40,40);    
  18.     
  19.         //Call the method addHeaderAndFooter() to add header and footer    
  20.         addHeaderAndFooter(doc, PdfPageSize.A4, margin);    
  21.     
  22.         //Add two pages to the PDF document and draw string to it.    
  23.         PdfPageBase page1 = doc.getPages().add();    
  24.         PdfPageBase page2 = doc.getPages().add();    
  25.         PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 14));    
  26.         String text1 = "Demo of Spire.PDF";    
  27.         String text2 = "How to add header and footer to PDF in JAVA";    
  28.         page1.getCanvas().drawString(text1, font, PdfBrushes.getBlack(),0,0);    
  29.         page2.getCanvas().drawString(text2, font, PdfBrushes.getBlack(),0,0);    
  30.           
  31.         //Save the document    
  32.         doc.saveToFile("output/headerFooter.pdf");    
  33.         doc.close();    
  34.     
  35.     }    
  36. static void addHeaderAndFooter(PdfDocument doc, Dimension2D pageSize, PdfMargins margin) {    
  37.     
  38.       PdfPageTemplateElement header = new PdfPageTemplateElement(margin.getLeft(), pageSize.getHeight());    
  39.       doc.getTemplate().setLeft(header);    
  40.     
  41.       PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.getWidth(), margin.getTop());    
  42.       topSpace.setForeground(true);    
  43.       doc.getTemplate().setTop(topSpace);    
  44.     
  45.       //Draw header label    
  46.       PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12));    
  47.     
  48.       PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);    
  49.       String label = "E-iceblue Co.,Ltd";    
  50.       Dimension2D dimension2D = new Dimension();    
  51.       dimension2D.setSize(font.measureString(label, format));    
  52.       float y = topSpace.getHeight() - font.getHeight() - 1;    
  53.       PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 0.75f);    
  54.       topSpace.getGraphics().setTransparency(0.5f);    
  55.       topSpace.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);    
  56.       y = y - 1 - (float) dimension2D.getHeight();    
  57.       topSpace.getGraphics().drawString(label, font, PdfBrushes.getBlack(), margin.getLeft(), y, format);    
  58.     
  59.       PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.getRight(), pageSize.getHeight());    
  60.       doc.getTemplate().setRight(rightSpace);    
  61.     
  62.     
  63.       //Draw dynamic fields as footer    
  64.       PdfPageTemplateElement footer = new PdfPageTemplateElement(pageSize.getWidth(), margin.getBottom());    
  65.       footer.setForeground(true);    
  66.       doc.getTemplate().setBottom(footer);    
  67.     
  68.       y = font.getHeight() + 1;    
  69.       footer.getGraphics().setTransparency(0.5f);    
  70.       footer.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);    
  71.       y = y + 1;    
  72.       PdfPageNumberField pageNumber = new PdfPageNumberField();    
  73.       PdfPageCountField pageCount = new PdfPageCountField();    
  74.       PdfCompositeField pageNumberLabel = new PdfCompositeField();    
  75.       pageNumberLabel.setAutomaticFields(new PdfAutomaticField[]{pageNumber, pageCount});    
  76.       pageNumberLabel.setBrush(PdfBrushes.getBlack());    
  77.       pageNumberLabel.setFont(font);    
  78.       format = new PdfStringFormat(PdfTextAlignment.Right);    
  79.       pageNumberLabel.setStringFormat(format);    
  80.       pageNumberLabel.setText("page {0} of {1}");    
  81.       pageNumberLabel.setBounds(footer.getBounds());    
  82.       pageNumberLabel.draw(footer.getGraphics(), pageSize.getWidth() - margin.getLeft(), y);    
  83.   }    
  84.     
  85. }   
Here is an effective screenshot after adding header and footer to the new PDF document in the Java application.
 
Programmatically Create PDF Header And Footer In Java