1.首先导入依赖jar包
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
maven仓地址:https://mvnrepository.com/search?q=pdfbox
2.代码片段
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
public class Test {
public static void main(String[] args) throws InvalidPasswordException, IOException {
File file = new File("C:\\Users\\Admin\\Desktop\\test.pdf");
PDDocument pdDocument = PDDocument.load(file);
// 方法一
int pages = pdDocument.getNumberOfPages();
System.out.println("pdf文件的总页数为:" + pages);
// 方发二
int pages2 = pdDocument.getPages().getCount();
System.out.println("pdf文件的总页数为:" + pages2);
// 关闭资源
pdDocument.close();
}
}
欢迎点赞收藏