Java adds page numbers to PDF documents

In daily work, in order to facilitate reading, retrieving and managing PDF documents, we often need to add page numbers. Then share a free Spire.PDF For Java is a quick way to add page numbers to PDF documents in Java programs.

 

First of all, put a rendering for your reference:

 

 

Basic steps:

  1. download Free Spire.PDF for Java Package and extract
  2. Under the lib folder Spire.Pdf.jar Packages are imported into Java applications as dependencies, or JAR packages are installed through Maven repositories (configuration pom.xml The code of the document is as follows)
  3. Create a new Java Class in the Java application (here I name it AddPageNumbers), enter the corresponding java code and run

 

to configure pom.xml File:

 

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

 

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

    public static void main(String[] args) {

        //load PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("sample.pdf");

        //Create font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Tahoma", Font.PLAIN, 10),true);

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

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

        //Traversing pages in a document
        for (int i = 0; i < pdf.getPages().getCount(); i++) {

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

            //Initialize total pages field
            PdfPageCountField count = new PdfPageCountField();

            //Create composite domain
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "The first{0}Page of{1}page", number, count);

            //Set text alignment within compound fields
            compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));

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

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

            //Add composite domain to PDF page
            compositeField.draw(pdf.getPages().get(i).getCanvas());
        }

        //Save as another document
        pdf.saveToFile("AddPageNumbers.pdf");
    }

Tags: Java Maven xml

Posted on Fri, 29 May 2020 10:35:20 -0400 by cLFlaVA