DEV Community

loading...

Add Header and Footer to an Existing PDF Document in Java

eiceblue profile image E-iceblue Product Family ・2 min read

PDF header or footer is a good place to display extra information about the document, for instance, you can insert a logo, contact information and page numbers to header or footer so that your document looks more professional and informative. In this article, I’ll show you how to add images, text, lines and page numbers to an exising PDF document at the header and footer spaces by using Free Spire.PDF for Java.

Spire.PDF doesn’t provide a method to directly apply header or footer to a PDF document. In order to do so, I created the drawHeader method and the drawFooter method. The following code snippet works for most PDF documents converted from regular Word documents where the page margin is Normal (top/bottom 72pt, right/left 90pt). If your PDF document has different margins, try to modify the values of X and Y to get a proper result.

Using the code

import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

public class InsertHeaderAndFooter {

    public static void main(String[] args) {

        //Load a PDF document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");

        //Draw header on the document
        drawHeader(doc);

        //Draw footer on the document
        drawFooter(doc);

        //Save the document 
        doc.saveToFile("output/InsertHeaderAndFooter.pdf");
    }

    public static void drawHeader(PdfDocument doc)
    {
        //Get page size
        Dimension2D pageSize = doc.getPages().get(0).getSize();

        //Declare two float variable
        float x = 90;
        float y = 20;

        for (int i = 0; i < doc.getPages().getCount(); i++)
        {
            //Draw image at the specified position
            PdfImage headerImage = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\your-logo-here.jpg");
            float width = headerImage.getWidth()/3;
            float height = headerImage.getHeight()/3;
            doc.getPages().get(i).getCanvas().drawImage(headerImage, x,  y, width, height);

            //Draw a line under the image
            PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
            doc.getPages().get(i).getCanvas().drawLine(pen, x, y + height + 2,  pageSize.getWidth() - x, y + height + 2);
        }
    }

    public  static void drawFooter(PdfDocument doc)
    {
        //Get page size
        Dimension2D pageSize = doc.getPages().get(0).getSize();

        //Declare two float variable
        float x = 90;
        float y = (float) pageSize.getHeight()- 72;

        for (int i = 0; i < doc.getPages().getCount(); i++)
        {
            //Draw a line at the specified position
            PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
            doc.getPages().get(i).getCanvas().drawLine(pen, x, y, pageSize.getWidth() - x, y);

            //Draw text at the specified position
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "Your Name Here\nTel:0101112222\nWebsite:www.yourwebsite.com";
            doc.getPages().get(i).getCanvas().drawString(footerText, font, PdfBrushes.getBlack(), x, y, format);

            //Draw page number
            PdfPageNumberField number = new PdfPageNumberField();
            PdfPageCountField count = new PdfPageCountField();
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}", number, count);
            compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));
            Dimension2D fontSize = font.measureString(compositeField.getText());
            compositeField.setBounds(new Rectangle2D.Float((float) (pageSize.getWidth() - x - fontSize.getWidth()), y, (float)fontSize.getWidth(), (float)fontSize.getHeight()));
            compositeField.draw(doc.getPages().get(i).getCanvas());
        }
    }
}

Output:
Alt Text

Discussion

pic
Editor guide