关于word转pdf功能实现

遇到word转pdf相关的问题,记录一下相关的使用

1 Spire

spire.doc是一款国产的,专业的 Java Word 组件,使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中.

其中的免费版本, 有特殊限制,在加载或保存 Word 文档时,要求 Word 文档不超过 500 个段落,25 个表格。同时将 Word 文档转换为 PDF 和 XPS 等格式时,仅支持转换前三页.

收费版本: 也可使用,首页页眉会出现警告信息,且最多支持十页,每页中间还有警告信息.

以免费版本演示为例:

1 添加依赖

        <repositories>
            <repository>
                <id>com.e-iceblue</id>
                <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
            </repository>
        </repositories>
    <dependencies>
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
     </dependencies>

2 测试

public class DemoController {
    
    
    public static void main(String[] args) {
    
    
        // word文档地址
        String docFile = "D:\\PublicSoftware\\jar\\2xs.docx";
        // 导出pdf地址
        String pdfFile = "D:\\PublicSoftware\\jar\\xs4.pdf";

        //实例化Document类的对象
        Document doc = new Document();
        //加载Word
        doc.loadFromFile(docFile);

        // 开始时间
        long begin = System.currentTimeMillis();
        //保存为PDF格式
        doc.saveToFile(pdfFile, FileFormat.PDF);
        // 结束时间
        long end = System.currentTimeMillis();
        System.out.println("word转pdf完成: 耗时 : "+ ((end-begin)/1000) + "秒");
    }
}   
// word转pdf完成: 耗时 : 6秒

2 poi

强大的poi,在文档处理方面,很全面,但是组件多,各种版本之间存在冲突.

查询网上一个可使用的一个版本如下:

1 添加依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
            <version>2.0.1</version>
        </dependency>

2 测试

public class DemoController {
    
    
    public static void main(String[] args) throws IOException {
    
    
        // 开始时间
        long begin = System.currentTimeMillis();
        String docFile = "D:\\PublicSoftware\\jar\\xs.docx";
        String pdfFile = "D:\\PublicSoftware\\jar\\xs6.pdf";
        
        InputStream doc = new FileInputStream(docFile);
        XWPFDocument document = new XWPFDocument(doc);
        PdfOptions options = PdfOptions.create();
        OutputStream out = new FileOutputStream(pdfFile);
        PdfConverter.getInstance().convert(document, out, options);
        doc.close();
        out.close();

        // 结束时间
        long end = System.currentTimeMillis();
            System.out.println("word转pdf完成: 耗时 : "+ ((end-begin)/1000) + "秒");
    }
}
// word转pdf完成: 耗时 : 15秒

3 总结

上述两个方法都可以将word转为pdf文件,各自都用特点.

Spire,依赖少,但免费版本限制过多,而收费版本价格不低,直接使用又有警告信息.(使用公司邮箱申请,可免费使用一个月,但不是长久之计);

poi文档处理,免费,但依赖多,且各个版本之间可能存在冲突,且对于生成pdf的一些排版,篇幅的处理,效果没有spire好.

猜你喜欢

转载自blog.csdn.net/ABestRookie/article/details/131303740