在开发某些业务系统的时候,可能会遇到需要
合并PDF文件
这样的需求,即将多个PDF文件合并成一个新的PDF文件,这个时候可以借助于
itextpdf
这个开源库进行实现,这个库在操作PDF方面还是很强大的。
下面通过具体的代码来演示。
1、引入依赖
目前
itextpdf
最新版本为
5.5.13.3
,可以在
https://search.maven.org/
网站进行搜索。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
2、代码实现
package com.magic.itextpdf;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
public class PdfUtils {
/**
* 合并PDF文件
* @param sourceFilePaths 需要合并的PDF文件路径列表
* @param destFilePath 合并后的新文件
*/
public static void mergePdfFile(List<String> sourceFilePaths, String destFilePath) {
if (sourceFilePaths == null || sourceFilePaths.isEmpty() || destFilePath == null) {
return;
}
Document document = null;
PdfCopy copy = null;
OutputStream os = null;
try {
// 创建合并后的新文件的目录
Path dirPath = Paths.get(destFilePath.substring(0, destFilePath.lastIndexOf(File.separator)));
Files.createDirectories(dirPath);
os = new BufferedOutputStream(new FileOutputStream(new File(destFilePath)));
document = new Document(new PdfReader(sourceFilePaths.get(0)).getPageSize(1));
copy = new PdfCopy(document, os);
document.open();
for (String sourceFilePath : sourceFilePaths) {
// 如果PDF文件不存在,则跳过
if (!new File(sourceFilePath).exists()) {
continue;
}
// 读取需要合并的PDF文件
PdfReader reader = new PdfReader(sourceFilePath);
// 获取PDF文件总页数
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (copy != null) {
try {
copy.close();
} catch (Exception ex) {
/* ignore */
}
}
if (document != null) {
try {
document.close();
} catch (Exception ex) {
/* ignore */
}
}
if (os != null) {
try {
os.close();
} catch (Exception ex) {
/* ignore */
}
}
}
}
}
合并PDF文件的本质其实就是新创建一个PDF文件,然后依次拷贝需要合并的PDF的每个页面,主要的关键类是
PdfCopy
,用于复制 PDF 页面,
PdfCopy
类图如下:
PdfCopy
类是一个比较常用的类,比如在旋转PDF、抽取PDF时都可能会用到,此处先不对
PdfCopy
类的源码做过多介绍,关于该类的更多应用可以阅读我的另外两篇文章:
3、测试验证
public static void main(String[] args) {
List<String> sourceFilePaths = Arrays.asList(
"D:\\Test\\test_1.pdf",
"D:\\Test\\test_2.pdf",
"D:\\Test\\test_3.pdf");
PdfUtils.mergePdfFile(sourceFilePaths, "D:\\Test\\test_merged.pdf");
}
运行后,可以看到在
D:\Test
目录下生成了
test_merged.pdf
文件,其中
test_1.pdf
、
test_2.pdf
、
test_3.pdf
都是两页,合并后的文件
test_merged.pdf
为6页,如下: