JAVA将多个Pdf合并成一个Pdf

Java将多个Pdf合并成一个文件

1.前言(吐槽一下)

人在公司,身不由己,需求总是层出不穷,哎~。前些天,公司要求将一个Html页面生成Pdf文件,并同时将其他Word打包通过Email发送出去。今天又收到需求,文件太多,不好,希望把文件合并成一个,没办法公司需求就是天,开搞。

打开百度,输入“Java合并Pdf”,打开十几个网页,经过十多位大佬的传授,终于完成,并采用了一种速度最快的方法——Itextpdf。

2.Itextpdf介绍

iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。(本段来源于百度百科-)itext有丰富的pdf操作方法,完全可以满足技术需要。

3.代码实现

先在pom中引入依赖
<dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>itextpdf</artifactId>
     <version>5.5.13.3</version>
</dependency>

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;

/**
 * @author: YSL
 * @date: 2022/8/2 16:16
 */
public class MergePdfFiles {

    private static final Logger logger = LoggerFactory.getLogger(MergePdfFiles.class);
    /**
     * 将多个pdf合并成一个pdf文件
     *
     * @auther: YSL
     * @date: 2022/8/2 16:46
     * @param pdfFilesPath  全路径
     * @param mergeFilePath  全路径
     */
    public static Boolean mergePdf(List<String> pdfFilesPath, String mergeFilePath){
        Document document = null;
        ByteArrayOutputStream os = null;
        try {
            // 获取纸张大小并实例化一个新的空文档, 例如 A5 纸
            document = new Document(new PdfReader(pdfFilesPath.get(0)).getPageSize(1));
            os = new ByteArrayOutputStream();
            // 实例化复制工具
            final PdfCopy copy = new PdfCopy(document, os);
            // 打开文档准备写入内容
            document.open();
            // 循环所有pdf文件
            for (String s : pdfFilesPath) {
                // 读取pdf
                final PdfReader reader = new PdfReader(new FileInputStream(s));
                // 获取页数
                final int numberOfPages = reader.getNumberOfPages();
                // pdf的所有页, 从第1页开始遍历, 这里要注意不是0
                for (int i = 1; i <= numberOfPages; i++) {
                    document.newPage();
                    // 把第 i 页读取出来
                    final PdfImportedPage page = copy.getImportedPage(reader, i);
                    // 把读取出来的页追加进输出文件里
                    copy.addPage(page);
                }
                reader.close();
            }
            //输出到指定目录文件中
            FileOutputStream fos = new FileOutputStream(mergeFilePath);
            fos.write(os.toByteArray());
            fos.close();
            copy.close();

            return true;
        } catch (IOException | DocumentException e) {
            logger.info("Pdf合并发生异常",e);
            return false;
        } finally {
            if (document != null) {
                document.close();
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.info("Pdf合并发生异常",e);
                }
            }
        }
    }
}

Document新建一个空白Pdf,通过PdfReader挨个读取要合并的Pdf文件,用PdfCopy工具将PdfImportedPage读取到每一页Pdf按顺序放入空白Pdf中,即可完成合并操作。

4.结束

生活如此美好,方法备份就好 -,如果有用,可以收藏插眼-

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值