https://blog.csdn.net/qq_21344887/article/details/120913490
/**
* 合并2个文档
* @param bytes1 文档1的字节数组
* @param bytes2 文档2的字节数组
* @return
*/
public static byte[] combinePDF(byte[] bytes1, byte[] bytes2) {
PdfDocument pdf = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdf = new PdfDocument(new PdfWriter(baos));
//定义合并文档
PdfMerger merger = new PdfMerger(pdf);
//添加第一个文档
PdfDocument firstSourcePdf = null;
try {
firstSourcePdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(bytes1)));
} catch (IOException e) {
e.printStackTrace();
}
merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
//添加第二个文档
PdfDocument secondSourcePdf = null;
try {
secondSourcePdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(bytes2)));
} catch (IOException e) {
e.printStackTrace();
}
merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
// 合并并关闭
merger.close();
firstSourcePdf.close();
secondSourcePdf.close();
pdf.close();
byte[] bytes = baos.toByteArray();
return bytes;
}
合并文档 记录一下