原创

【Java】PDFBox 一个开源的PDF支持库

前言

因为 billhelp 项目需要增加一个pdf合并的功能,故而有了这次的改造之旅
这里我用了pdfbox支持库,

The Apache PDFBox library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. PDFBox also includes several command line utilities. PDFBox is published under the Apache License, Version 2.0.

maven引用

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.26</version>
</dependency>

gradle引用

// https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.26'

代码示例

    /** * 合并pdf * @return */
    private static int mergePDF(String path) throws IOException{

        PDFMergerUtility pdfMerger= new PDFMergerUtility();
        File f=new File(path);

        File[] pdffiles=f.listFiles();

        for (File file : pdffiles) {
            pdfMerger.addSource(file);
        }

        pdfMerger.setDestinationFileName(path+"\\merger.pdf");
        pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
        return 0;
    }

参考

Maven Repository
Getting Started
合并pdf示例

本文来自:【Java】PDFBox 一个开源的PDF支持库-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2022年10月27日,已超过 770 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
本文目录