[Java] Java adds page number to existing PDF document

1 minute read

In daily work, it is often necessary to add page numbers to PDF documents to make them easier to read, search, and manage. So I will show you how to use Free Spire.PDF for Java to quickly add page numbers to a PDF document in a Java program.

First, place the result graph for reference: page numbers.png

Basic steps: 1. Download and unzip the Free Spire.PDF for Java package. 2. Import the Spire.Pdf.jar package in the lib folder as a dependency into your Java application. (You can also install the JAR package directly from the Maven repository (see below for the code that makes up the pom.xml file)). .. 3. In your Java application, create a new Java Class (here named AddPageNumbers), enter the corresponding Java code and run it.

Configure the pom.xml file:

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.pdf.free</artifactId>
      <version>2.6.3</version>
   </dependency>
</dependencies>

Java code example:

import com.spire.pdf.PdfDocument;
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 PDF document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("file1.pdf");

        // create font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Song font", Font.PLAIN, 10),true);

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

        // initialize the y coordinate
        float y = (float) pageSize.getHeight()-40;

        // iterate over the pages of the document
        for (int i = 0; i <doc.getPages().getCount(); i++) {

            // Initialize page number field
            PdfPageNumberField number = new PdfPageNumberField();

            // Initialize the total page count field
            PdfPageCountField count = new PdfPageCountField();

            // create a composite domain
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0}Total {1}", number, count);

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

            // Character size measurement
            Dimension2D textSize = font.measureString(compositeField.getText());

            //Set the position and size of the composite field on the PDF page
            compositeField.setBounds(new Rectangle2D.Float(((float) pageSize.getWidth()-(float) textSize.getWidth())/2, y, (float) textSize.getWidth(), (float) textSize.getHeight() ));

            //Add a compound field to the PDF page
            compositeField.draw(doc.getPages().get(i).getCanvas());
        }

        // save the document
        doc.saveToFile("Numbers.pdf");
    }
}