maven依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>1.8.10</version>
</dependency>
代码
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
public static void main(String[] args) {
String savePath="";//文件路径
String pdfPath ="";//文件保存路径
cutPdf(savePath,pdfPath);
}
private static void cutPdf(String savePath,String pdfPath) {
File file = new File(savePath);
File[] files = file.listFiles();
for (File f:files) {
PDDocument document = new PDDocument();
try {
document = PDDocument.load(f);
} catch (Exception e) {
e.printStackTrace();
}
int noOfPages = document.getNumberOfPages();
System.out.println(f.getName()+" page:"+noOfPages);
document.removePage(noOfPages - 1);//removePage 传入 0 表示删除第1页 类推
try {
document.save(pdfPath+f.getName());
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Jar地址
链接:https://pan.baidu.com/s/1BmOHbUvNjnkL1B0ONxl0Vg
提取码:njb1