Code compilation environment
IntelliJ IDEA 2019 (jdk 1.8.0)
PDF Jar package: Free Spire.PDF for Java 5.1.0
Introduced jarPackage
Import method 1:
Manual introduction. 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 introduce the jar file in the local path into the Java program:
Import method 2: If you want to install through Maven, you can install it in pom.xml Add the following code to the file to import the JAR file.
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-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 PDF document
The following steps show how to add page numbers like "Page X of Y" to an existing PDF document:
Create an object of PdfDocument class.
Use the PdfDocument.loadFromFile() method to load the PDF document.
Create an object of PdfPageNumberField class.
Create an object of PdfPageCountField class.
Create an object of PdfCompositeField class.
Use the PdfCompositeField.setStringFormat() method to set the text alignment for a composite field.
Loop through each page in the PDF document and then use the PdfCompositeField.draw() method to draw the composite field at a specific location on the page.
Use the PdfDocument.saveToFile() method to save the resulting document.
Complete 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 addPageNumber { public static void main(String[] args) { //创建 PdfDocument 类的对象 PdfDocument pdf = new PdfDocument(); //载入PDF文档 pdf.loadFromFile("九寨沟简介 .pdf"); //创建 PdfTrueTypeFont 类的对象 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("华文中宋", Font.PLAIN, 10)); //创建 PdfPageNumberField 类的对象 PdfPageNumberField pageNumberField = new PdfPageNumberField(font, PdfBrushes.getBlack()); //创建 PdfPageCountField 类的对象 PdfPageCountField pageCountField = new PdfPageCountField(font, PdfBrushes.getBlack()); //创建一个 PdfCompositeField 类的对象,将页码字段和页数字段添加到复合字段 PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页/共{1}页", pageNumberField, pageCountField); //设置复合字段的文字格式 compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top)); //循环遍历文档中的页面 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(); //将复合字段描绘于每个页面上 compositeField.draw(page.getCanvas(), x, y); } //保存结果文档 pdf.saveToFile("添加页码.pdf"); } }
Rendering
The above is the detailed content of How to add page numbers to PDF documents in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment