以下代码是将 test.pdf 文件拆分成多个文件,且拆分出来的文件命名规则是 :当前系统时间-序号。
// 根据页码拆分pdf,有多少页就拆成多少张pdf
File file = new File("D:/test.pdf");
PdfReader pdfReader = new PdfReader(new FileInputStream(file));
int pages = pdfReader.getNumberOfPages();
// 设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
for (int i = 1; i <= pages; i++) {
PdfReader reader = new PdfReader("D:/test.pdf");
Document doc = new Document();
// 每个pdf命名规则:时间戳(2019109143408)+"-"+顺序编号(i)
String pdfName = "D:/" + df.format(new Date()) + "-" + i + ".pdf";
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(pdfName));
doc.open();
PdfContentByte cb = writer.getDirectContent();
doc.newPage();
cb.addTemplate(writer.getImportedPage(reader, i), 0, 0);
doc.close();
writer.close();
reader.close();
}