代码相关的jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
代码演示
public static void spiltPDF(byte[] bytes, String newFile, int start, int end) {
Document document = null;
PdfCopy copy = null;
try {
PdfReader reader = new PdfReader(bytes);
int n = reader.getNumberOfPages();
if (end == 0) {
end = n;
}
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newFile));
document.open();
for (int j = start; j <= end; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("split pdf file error:" + e.getMessage());
}
}