使用itextpdf将多个PDF文件合并为一个PDF文件

在开发某些业务系统的时候,可能会遇到需要合并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.pdftest_2.pdftest_3.pdf都是两页,合并后的文件 test_merged.pdf 为6页,如下:
在这里插入图片描述

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值