我的依赖版本
com.itextpdf:itextpdf:5.5.13 (itextpdf-5.5.13.jar)
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
合并方法代码
/**
* 合并所有pdf
*
* @param fileList pdf文件列表
* @return 合并后的文件
*/
public File doMergeFile(List<File> fileList, String resFileName) {
if (fileList.size() == 0) {
return null;
}
File file = new File(resFileName);
// 合并
try {
Document document = new Document();
PdfCopy copy = new PdfCopy(document, Files.newOutputStream(file.toPath()));
document.open();
for (File file1 : fileList) {
PdfReader reader = new PdfReader(file1.getPath());
copy.addDocument(reader);
// copy.addPage(copy.getImportedPage(reader, 1)); // 按页合并
reader.close();
}
document.close();
} catch (Exception e) {
throw new RuntimeException("合并出错了", e);
}
// 删除无用文件
for (File value : fileList) {
FileUtil.del(value);
}
return file;
}