Java adds page numbers to existing PDF documents

In your daily work, you often need to add page numbers to your PDF documents to make them easier to read, search, and manage. So I'll show you how to use Free Spire.PDF for Java to quickly add page numbers to PDF documents in Java programs.

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

** Basic steps: ** ** 1. ** Free Spire.PDF for Java Download and unzip the package. ** 2. ** Import the Spire.Pdf.jar package in the lib folder into your Java application as a dependency. (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 (named AddPageNumbers here) and enter and execute the corresponding Java code.

** Configure the pom.xml file: **


** 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 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();

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

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

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

        //Iterates 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 complex domain
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "page{0}total{1}", number, count);

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

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

            //Set the position and size of compound fields on a 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 a PDF page

        //Save the document

Recommended Posts

Java adds page numbers to existing PDF documents
Java adds table to PDF
Java adds hyperlinks to Word documents
Java adds form fields to PDF
Launch Docker from Java to convert Office documents to PDF
Java applications convert Word (DOC / DOCX) documents to PDF
Java to extract PDF text content
Java adds SmartArt graphics to PowerPoint
Java draws shapes in PDF documents
How to find Java prime numbers
Add footnotes to Word documents in Java
Introduce Kotlin to your existing Java Maven Project
How to display a web page in Java
Java adds a text box to PowerPoint slides
Java adds and removes watermarks in word documents
[Java] Introduction to Java
Introduction to java