前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java实现word转pdf

java实现word转pdf

作者头像
华创信息技术
发布2022-05-28 10:24:21
2.7K0
发布2022-05-28 10:24:21
举报
文章被收录于专栏:华创信息技术华创信息技术

文章时间:2020-12-5 16:38:54 解决问题:java实现word转pdf

目前发现可用的实现方式有两种,一种是使用e-iceblue的免费版api,此方法最为简单但存在限制,导出页数不能超过三页。 另一种是使用openoffice,但较上一种方法麻烦一些,需要安装openoffice的软件,但没有导出限制,请根据自身需求自行选用。

方式一 e-iceblue的免费版api

官方文档https://www.e-iceblue.cn/spiredocforjavaconversion/java-convert-word-to-pdf.html

第一步 添加maven依赖及远程仓库

代码语言:javascript
复制
</dependencies>
   ...
   <dependency>
       <groupId>e-iceblue</groupId>
       <artifactId>spire.doc.free</artifactId>
       <version>3.9.0</version>
   </dependency>
   ...
</dependencies>
<repositories>
    ...
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
    ...
</repositories>

第二步 编写后台代码

参考代码如下

代码语言:javascript
复制
   // 模板文件路径
   String templateUrl = "C:\\Users\\dev\\Desktop\\template.docx";
   // word文件生成路径
   String generateUrl ="C:\\Users\\dev\\Desktop\\generate.pdf";

   Document document = new Document();
   document.loadFromFile(generateUrl);
   //保存生成的pdf
   document.saveToFile(generateUrl, FileFormat.PDF);

方式二 openoffice转换

官方文档:http://www.openoffice.org/why/index.html openoffice安装教程:http://wiki.nooss.cn/archives/405.html

第一步 添加maven依赖

需注意:此处引用的版本为2.2.1版本,不支持.docx文件的转换,若需要转换.docx文件需2.2.2及以上版本,但maven库没有此版本需自行下载导入jar包

代码语言:javascript
复制
        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.1</version>
        </dependency>

第二步 编写后台代码

代码语言:javascript
复制
        // word文件路径
        String sourceFile = "C:\\Users\\dev\\Desktop\\template.docx";
        // 生成的pdf路径
        String destFile = "C:\\Users\\dev\\Desktop\\generate.pdf";
        try {
            File inputFile = new File(sourceFile);

            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

            // 连接到运行在端口8100上的OpenOffice
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                    "127.0.0.1", 8100);
            connection.connect();

            // 文件转换
            DocumentConverter converter = new OpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);

            // 关闭连接
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-5 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方式一 e-iceblue的免费版api
    • 第一步 添加maven依赖及远程仓库
      • 第二步 编写后台代码
      • 方式二 openoffice转换
        • 第一步 添加maven依赖
          • 第二步 编写后台代码
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档