合并pdf压缩zip

package com.test.util.testTableWord;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
import com.test.exception.ErrorException;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class TestComparePdf {

    public static void main(String[] args) {
        String fileName = java.util.UUID.randomUUID().toString().replaceAll("-","") + ".pdf";
        List<String> pdfFilenames = new ArrayList<>();
        pdfFilenames.add("E:\\app\\ouput.pdf");
        pdfFilenames.add("E:\\app\\textWaterMark.pdf");
        try {
            combinPdf(pdfFilenames,fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> srcFiles = new ArrayList<>();
        srcFiles.add("E:\\app\\ouput.pdf");
        srcFiles.add("E:\\app\\textWaterMark.pdf");
        Map<String, String> fileNameMap = new HashMap<>();
        fileNameMap.put("E:\\app\\ouput.pdf","ouput");
        fileNameMap.put("E:\\app\\textWaterMark.pdf","textWaterMark");
        toZip(srcFiles,fileNameMap);
    }

    /***
     * 合并pdf文件
     * @param pdfFilenames
     * @param filename
     * @return
     * @throws Exception
     */
    public static String combinPdf(List<String> pdfFilenames, String filename) throws Exception {
        String targetFilename = "E:\\app\\" + filename;
        PdfReader reader = null;
        Document doc = new Document();
        PdfCopy pdfCopy = new PdfCopy(doc, new FileOutputStream(targetFilename));
        int pageCount = 0;
        doc.open();
        if (pdfFilenames.size()>0) {
            for (int i = 0; i < pdfFilenames.size(); ++i) {
                reader = new PdfReader(pdfFilenames.get(i));
                pageCount = reader.getNumberOfPages();
                for (int j = 1; j <= pageCount; ++j) {
                    pdfCopy.addPage(pdfCopy.getImportedPage(reader, j));
                }
            }
        }
        doc.close();
        return targetFilename;
    }


    public static String toZip(List<String> srcFiles, Map<String, String> fileNameMap) throws RuntimeException {
        long start = System.currentTimeMillis();
        String fileName =  "E:\\app\\" + java.util.UUID.randomUUID().toString() + ".zip";
        ZipOutputStream zos = null;
        try {
            FileOutputStream out = new FileOutputStream(fileName);
            zos = new ZipOutputStream(out);
            for (String srcFile : srcFiles) {
                byte[] buf = new byte[4096];
                zos.putNextEntry(new ZipEntry(fileNameMap.get(srcFile)+".pdf"));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                in.close();
            }
            zos.setComment("导出文件");
            zos.flush();
            zos.closeEntry();
            long end = System.currentTimeMillis();
            log.info("导出文件打包完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new ErrorException(400, "打包文件时出错:"+e.toString());
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    log.error("打包文件时出错:", e);
                }
            }
        }
        return fileName;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值