How to Add Page Numbers to Existing PDF Documents through Java code?

The page number allows for a clear understanding of the total number of pages, a quick search for the paragraph you want, and printing without blurring the top, middle, and bottom. Many people choose PDF for document saving when using office software, because PDF does not cause garbled code or even formatting changes after saving. After successfully adding page numbers to the PDF, the bottom of each PDF page will display page numbers, which can provide a more detailed and clear understanding of the relevance of the document and the specific location of the content. This also facilitates our subsequent work and greatly improves work efficiency in the workplace. Today's article will introduce how to programmatically add page numbers to PDF documents through Java code. Please read the following for details.

Programming Environment

Method 1:

Introduced manually. Download Free Spire.PDF for Java locally, unzip it, and find the Spire.PDF.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <name>e-iceblue</name>

        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>

    </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.pdf.free</artifactId>

        <version>5.1.0</version>

    </dependency>

</dependencies>

Add Page Numbers to Existing PDF Documents

The following steps show you how to add "Page X of Y" page numbers to an existing PDF document:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Initialize an instance of PdfPageNumberField class.
  • Initialize an instance of PdfPageCountField class.
  • Initialize an instance of PdfCompositeField class.
  • Set the text alignment for the composite field using PdfCompositeField.setStringFormat() method.
  • Loop through each page in the PDF document, then draw the composite field on the specific location of the page using PdfCompositeField.draw() method.
  • Save the result document using PdfDocument.saveToFile() method.

Full Code

Java

import com.spire.pdf.*;
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.*;

public class
AddPageNumbers {
   
public static void main(String []args){
       
//Create a PdfDocument instance
       
PdfDocument pdf = new PdfDocument();
       
//Load the PDF document
       
pdf.loadFromFile("Sample.pdf");

       
//Create a PdfTrueTypeFont instance
       
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10));

       
//Create a PdfPageNumberField instance
       
PdfPageNumberField pageNumberField = new PdfPageNumberField(font, PdfBrushes.getBlack());

       
//Create a PdfPageCountField instance
       
PdfPageCountField pageCountField = new PdfPageCountField(font, PdfBrushes.getBlack());

       
//Create a PdfCompositeField instance, add the page number filed and the page count field to composite filed
       
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}", pageNumberField, pageCountField);

       
//Set string format for the composite field
       
compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top));

       
//Loop through the pages
       
for(int i = 0; i < pdf.getPages().getCount();i ++)
        {
            PdfPageBase page = pdf.getPages().get(i)
;
            float
x = (float) page.getSize().getWidth()/2 - 20;
            float
y = (float)page.getSize().getHeight() - pdf.getPageSettings().getMargins().getBottom();
           
//Draw the composite filed on each page
           
compositeField.draw(page.getCanvas(), x, y);
       
}

       
//Save the result document
       
pdf.saveToFile("AddPageNumbers.pdf");
   
}
}

Effective Shot

Conclusion:

In this article, you have learned how to convert PDF to Word in Java. Not only that, we also have other functions, such as, Java: Rotate Pages in PDF, Java: Rearrange Pages in PDF, Java: Split a PDF Page into Multiple Pages  and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for Java.

 















Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application