栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java合并PDF文件方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java合并PDF文件方式

需求说明:

需要将多个PDF文件或者文件流合并为一个文件

第一种方式

使用e-iceblue 官网地址。注意:这是收费软件,合并的文件会有水印。

在pom.xml文件中引入Jar包


    
        com.e-iceblue
        e-iceblue
        https://repo.e-iceblue.com/nexus/content/groups/public/
    



     e-iceblue 
    spire.pdf
    5.4.0

private static void mergePDF() throws FileNotFoundException {
        File file1 = new File("/Users/tianming/Downloads/附件.pdf");
        File file2 = new File("/Users/tianming/Downloads/附件.pdf");
        String outputFile = "/Users/tianming/Downloads/合并附件.pdf";
        FileInputStream stream1 = new FileInputStream(file1);
        FileInputStream stream2 = new FileInputStream(file2);
        //加载PDF示例文档
        InputStream[] streams = new FileInputStream[]{stream1, stream2};
        //合并PDF文档
        PdfDocumentBase doc = PdfDocument.mergeFiles(streams);
        //保存文档
        doc.save(outputFile);
        doc.close();
    }
第二种方式

使用pdfbox依赖 apache官网


  org.apache.pdfbox
  pdfbox
  2.0.22

private static void mergePDFbox(List list, String outputFile){
    // 合并PDF文档法二
    File outFile = new File(outputFile);
    PDFMergerUtility merger = new PDFMergerUtility();
    list.forEach(merger::addSource);
    merger.setDestinationFileName(outFile.getName());
    try {
        // 合并PDF
        merger.mergeDocuments(null);
    } catch (IOException e) {
        log.error("Pdf Merge error: ", e);
    }
}
第三种方式

使用itext 官方地址


   com.itextpdf
    itextpdf
    5.5.13.3

private static byte[] mergePdfFiles(List inputStreams, String outFile) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Document document = new Document();// 创建一个新的PDF
        byte[] pdfs = new byte[0];
        try {
            PdfCopy copy = new PdfCopy(document, bos);
            document.open();
            for (InputStream is : inputStreams) {// 取出单个PDF的数据
                PdfReader reader = new PdfReader(stream2Byte(is));
                int pageTotal = reader.getNumberOfPages();
                for (int pageNo = 1; pageNo <= pageTotal; pageNo++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, pageNo);
                    copy.addPage(page);
                }
                reader.close();
            }
            document.close();
            pdfs = bos.toByteArray();
            bos.close();
            copy.close();
            FileCopyUtils.copy(pdfs, new File(outFile));
        } catch (DocumentException | IOException e) {
            log.error("合并PDF出错:" + e);
        }
        return pdfs;
    }

    private static byte[] stream2Byte(InputStream inputStream) {
        byte[] buffer = new byte[0];
        try (InputStream fis = inputStream; ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (IOException e) {
        }
        return buffer;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981239.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号