多个pdf合并

1、调用实例

public void packdownload(HttpServletResponse response) throws IOException {
    List<String> paths = new ArrayList<>();
    paths.add("E:\\aaa.pdf");
    paths.add("E:\\bbb.pdf");
    paths.add("E:\\ccc.pdf");
    if(paths.size() == 0){
        return;
    }
    // 合并pdf
    File result = mergePdf(paths);
    //拼接下载默认名称并转为ISO-8859-1格式
    String fileName = new String(("附件.pdf").getBytes(),"ISO-8859-1");
    response.setHeader("Content-Disposition", "attchment;filename="+fileName);
    //该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
    ServletOutputStream outputStream = response.getOutputStream();
    FileInputStream inputStream = new FileInputStream(result);
    // 如果是SpringBoot框架,在这个路径
    // org.apache.tomcat.util.http.fileupload.IOUtils产品
    // 否则需要自主引入apache的 commons-io依赖
    // copy方法为文件复制,在这里直接实现了下载效果
    IOUtils.copy(inputStream, outputStream);
    // 关闭输入流
    inputStream.close();
    //下载完成之后,删掉这个文件
    result.delete();
}
     
/**
 * 合并pdf
 *
 * @param filePaths
 * @return
 */
 private File mergePdf(List<String> filePaths) {
     File file = new File("E:\\", String.format("pdf/%s.pdf", UUID.randomUUID().toString()));
      PdfUtils.mergePDF(filePaths, file);
      return file;
 }

2、工具类

package io.zcy.common.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PdfUtils {

    public static void main(String[] args) {
        List<String> fileList = new ArrayList<>();
        fileList.add("D:\\桌面\\test\\pdf\\测试1.pdf");
        fileList.add("D:\\桌面\\test\\pdf\\测试.pdf");

        mergePDF(fileList, "");

    }


    public static boolean mergePDF(List<String> fileList, String newFile) {
        return mergePDF(fileList, new File(newFile));
    }

    public static boolean mergePDF(List<String> fileList, File newFile) {
        Document document = null;
        FileOutputStream newf = null;
        PdfCopy copy = null;
        try {
            FileInputStream inputStream = new FileInputStream(new File(fileList.get(0)));
            //其中new PdfReader() 的参数可以是文件路径,或者是FileInputStream 输入流,或者是byte[] 我这里采用的是输入流
            document = new Document(new PdfReader(inputStream).getPageSize(1));
            newf = new FileOutputStream(newFile);
            copy = new PdfCopy(document, newf);
            document.open();
            for (int i = 0; i < fileList.size(); i++) {
                File file=new File(fileList.get(i));
                if (!file.exists()){
                    continue;
                }
                FileInputStream input = new FileInputStream(file);
                PdfReader reader = new PdfReader(input);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            copy.flush();
            copy.close();
            try {
                newf.flush();
                newf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            document.close();
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阁下大名

您的鼓励就是我前进的动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值