java对pdf文件分页拆分
文章目录
- pdf文件拆分
- 指定分页大小
pdf文件拆分
- 导入依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.32</version>
</dependency>
2. 大文件拆分
public static boolean splitPdf() {
try {
long st = System.currentTimeMillis();
log.info("文件拆分进度 start");
File inputFile = new File("D:\\pdftest\\bigpdf.pdf"); // 输入的PDF文件路径
PDDocument document = PDDocument.load(inputFile);
int numberOfPages = document.getNumberOfPages();
for (int i = 0; i < numberOfPages; i++) {
log.info("文件拆分进度 " + (i + 1) + "/" + numberOfPages);
PDDocument splitDocument = new PDDocument();
PDPage page = document.getPage(i);
splitDocument.addPage(page);
String outputFilePath = "D:\\pdftest\\test\\output_" + (i + 1) + ".pdf"; // 输出文件的路径和名称
splitDocument.save(outputFilePath);
splitDocument.close();
}
document.close();
log.info("文件拆分进度 end");
long end = System.currentTimeMillis();
log.info("拆分和检测总耗时:" + (end - st) + " ms");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
指定分页大小
可以根据需要,决定将哪些分页放在一个文件里面。