public boolean mergePDFFiles(List<String> fileList,String mergrPdfPath) {
debugUtil.begin();
boolean result = false;
Document document = null;
PdfReader reader = null;
PdfCopy copy = null;
try {
document = new Document(new PdfReader(fileList.get(0)).getPageSize(1));
copy = new PdfCopy(document,new FileOutputStream(mergrPdfPath));
document.open();
for (int i = 0; i < fileList.size(); i++) {
reader = new PdfReader(fileList.get(i));
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
result = true;
}catch (Exception e) {
result = false;
throw new RuntimeException(e);
} finally {
if (document!=null) {
document.close();
}
if (reader!=null) {
reader.close();
}
if (copy!=null) {
copy.close();
}
}
debugUtil.end();
debugUtil.writeLog("合并pdf",null);
return result;
}
|