Maven依赖
官方文档
<!-- PDF拆分与合并工具-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.22</version>
</dependency>
测试代码
package com;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
public class PDFSplit {
public static void main(String[] args) throws IOException {
Splitter splitter = new Splitter();
// 设置起始页、结束页,每个文件的页数
splitter.setStartPage(5);
splitter.setEndPage(6);
splitter.setSplitAtPage(2);
PDDocument source = PDDocument.load(new File("C:/Users/Administrator/Desktop/文件名"));
List<PDDocument> list = splitter.split(source);
int i = 0;
for (PDDocument document : list) {
i++;
document.save("C:/Users/Administrator/Desktop/" + "out" + i + ".pdf");
}
}
}