合并PDF
/**
* 合并PDF
* @param list 输入流 列表
* @return 输出流
*/
public static ByteArrayOutputStream mergerPDF(List<InputStream> list) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, stream);
document.open();
for (InputStream is : list) {
PdfReader reader = new PdfReader(is);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
copy.addPage(copy.getImportedPage(reader, i));
}
reader.close();
}
copy.close();
document.close();
return stream;
}
拆分PDF
/**
* 拆分PDF
* @param is 输入流
* @return 输出流 列表
*/
public static List<ByteArrayOutputStream> splitPDF(InputStream is) throws IOException {
PdfReader reader = new PdfReader(is);
List<ByteArrayOutputStream> result = new ArrayList<>();
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Document document = new Document();
PdfSmartCopy copy = new PdfSmartCopy(document, stream);
document.open();
copy.addPage(copy.getImportedPage(reader, i));
copy.close();
document.close();
result.add(stream);
}
reader.close();
return result;
}