Home  >  Article  >  Java  >  How to add page numbers to PDF documents in Java

How to add page numbers to PDF documents in Java

PHPz
PHPzforward
2023-04-30 10:58:061101browse

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:

How to add page numbers to PDF documents in Java

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

How to add page numbers to PDF documents in Java

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete