Monday, 16 September 2019 07:23

Add Page Numbers to Existing PDF Documents in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to add page numbers to an existing PDF document at the footer space using Spire.PDF for Java. You can determine where to insert page numbers according to your own requirement.

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 AddPageNumbers {

    public static void main(String[] args) {

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

        //create a true type font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", Font.PLAIN, 10));

        //get the page size
        Dimension2D pageSize = doc.getPages().get(0).getSize();

        //declare a float variable
        float y = (float) pageSize.getHeight() - 72;

        //loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //initialize PdfPageNumberField instance
            PdfPageNumberField number = new PdfPageNumberField();

            //initialize PdfPageCountField instance
            PdfPageCountField count = new PdfPageCountField();

            //create PdfCompositeField instance
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}", number, count);

            //set the text alignment within the composite field
            compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));

            //get the text size
            Dimension2D textSize = font.measureString(compositeField.getText());

            //set the position and size of composite field
            compositeField.setBounds(new Rectangle2D.Float((float) pageSize.getWidth() - (float) textSize.getWidth() - 80, y, (float) textSize.getWidth(), (float) textSize.getHeight()));

            //draw composite filed on PDF page
            compositeField.draw(doc.getPages().get(i).getCanvas());
        }

        //save to another file
        doc.saveToFile("output/AddPageNumbers.pdf");
    }
}

Add Page Numbers to Exsiting PDF Documents in Java

Additional Info

  • tutorial_title:
Last modified on Wednesday, 18 September 2019 03:40