java获取pdf文件的总页数

时间:2024-01-31 09:06:15

1.在pom.xml加入依赖

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>1.8.11</version>
        </dependency>

2.测试代码:

package api.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.lowagie.text.pdf.PdfReader;

public class PdfPageTest {
  public static void main(String[] args) throws FileNotFoundException, IOException {
    File file = new File("d:\\PRE-CON281100007-add-1546498235473.pdf");
    PdfReader pdfReader = new PdfReader(new FileInputStream(file));
    int pages = pdfReader.getNumberOfPages();

    System.out.println("pdf文件的总页数为:" + pages);
  }
}