Page number usually appears on the white margins of a PDF page, helping readers remember where to start next time when they open it. Adding page numbers to your PDF document can make it more professional and easier to read. In this article, I am going to introduce how to add page numbers to an existing PDF document by using Spire.PDF for Java.
Spire.PDF offers important classes under the AutomaticFields namespace, some of them are listed in the table below, to automatically calculate the current page number, total page number, date time as well as section number. A special class named PdfCompsiteField is provided to concatenate two or more automatic fields into a single string. With the help of it, you can customize the page number in the form of “Page X of Y”, “X/Y”, or “Page X, Section Y”, etc.
Class | Description |
---|---|
PdfPageNumberField | Represents page number field. |
PdfPageCountField | Represents total page count automatic field. |
PdfCompositeField | Represents class which can concatenate multiple automatic fields into single string. |
Installing Spire.Pdf.jar
If you use Maven, you can easily import the Spire.Pdf.jar in your application by adding the following code to your project’s pom.xml file. For non-Maven projects, download the jar file from this link and manually add it as a dependency in your application.
<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</artifactId>
<verson>4.8.7</version>
</dependency>
</dependencies>
Using the code
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 AddPageNumbersToPdf {
public static void main(String[] args) {
//Load a PDF document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 11));
//Get the page size
Dimension2D pageSize = doc.getPages().get(0).getSize();
//Loop through the pages
for (int i = 0; i < doc.getPages().getCount(); i++) {
//Create a PdfPageNumberField object
PdfPageNumberField number = new PdfPageNumberField();
//Create a PdfPageCountField object
PdfPageCountField count = new PdfPageCountField();
//Create a PdfCompositeField object
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,
(float) pageSize.getHeight() - 72,
(float) textSize.getWidth(),
(float) textSize.getHeight())
);
//Draw composite filed on PDF page
compositeField.draw(doc.getPages().get(i).getCanvas());
}
//Save to file
doc.saveToFile("output/AddPageNumbers.pdf");
}
}
Top comments (0)