一丶添加itextpdf包
<!-- pdf文件操作包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
二丶编写工具类
public class PdfFileUtil {
/**
* 截取pdf第几页到第几页生成新的PDF
*
* @param realPath 需进行操作的文件路径,全路径(E:/text.pdf)
* @param savePath 需保存新的pdf路径, 全路径
* @param from 初始页
* @param end 末尾页
*/
public static void splitPDFFile(String realPath, String savePath, int from, int end) throws Exception {
Document document;
PdfCopy copy;
// 读取文件
PdfReader reader = new PdfReader(realPath);
// 获取总页数
int n = reader.getNumberOfPages();
if(end == 0){
end = n;
}
ArrayList<String> savepaths = new ArrayList<>();
savepaths.add(savePath);
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
document.open();
for(int j=from; j<=end; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
document.close();
}
}
end, 这里是选择初始页到末尾页生成新的文件