Java 使用openoffice进行word转换为pdf的方法步骤

 更新时间:2021年04月30日 09:24:35   作者:也许深情  
这篇文章主要介绍了Java 使用openoffice进行word转换为pdf的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、下载openoffice第三方工具

建议下载4.1.6版本
http://www.openoffice.org/download/index.html

二、开启openoffice服务

找到openoffice安装目录下OpenOffice 4\program>soffice运行cmd,运行命令soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

三、Java代码

package com.ry.controller;

import java.io.File;
import java.util.Date;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class PDTT {

    public static void main(String[] args) {
        // 找到openoffice安装目录下OpenOffice 4\program>soffice运行cmd
        // 开启open office命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

        // 获取开始时间
        Date startDate = new Date();
        // 目标文件(这里写需要被转换的文件地址和文件名)
        String sourceFile = "C:\\Users\\86199\\Desktop\\aaa.doc";
        // 生成的文件(这里写转换为pdf的文件地址和文件名)
        String destFile = "C:\\Users\\86199\\Desktop\\测试.pdf";
        try {
            // 运行转换方法
            System.out.println(office2PDF(sourceFile, destFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 获取结束时间
        Date endDate = new Date();
        System.out.println("总耗时:" + (endDate.getTime() - startDate.getTime()));

    }

    /*
        具体的转换方法
     */
    public static int office2PDF(String sourceFile, String destFile) throws Exception {
        try {
            File inputFile = new File(sourceFile);
            // 判断文件是否存在
            if (!inputFile.exists()) {
                System.out.println("源文件不存在");
                return -1;// 找不到源文件, 则返回-1
            }
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

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

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

            // 关闭连接
            connection.disconnect();
            // 执行成功
            System.out.println("转化成功");
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 失败时返回1
        return 1;
    }
}

waven仓库的配置依赖信息

  <!-- Apache Utils -->
     <dependency>
         <groupId>commons-beanutils</groupId>
         <artifactId>commons-beanutils</artifactId>
         <version>1.8.0</version>
     </dependency>
     <dependency>
         <groupId>commons-codec</groupId>
         <artifactId>commons-codec</artifactId>
         <version>1.5</version>
     </dependency>
     <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.2.1</version>
     </dependency>
     <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.4</version>
     </dependency>
     <dependency>
         <groupId>commons-io</groupId>
         <artifactId>commons-io</artifactId>
         <version>2.4</version>
     </dependency>
     <!-- openoffice-->
     <dependency>
         <groupId>com.artofsolving</groupId>
         <artifactId>jodconverter</artifactId>
         <version>2.2.1</version>
     </dependency>

     <dependency>
         <groupId>org.openoffice</groupId>
         <artifactId>ridl</artifactId>
         <version>4.1.2</version>
     </dependency>

     <dependency>
         <groupId>org.openoffice</groupId>
         <artifactId>jurt</artifactId>
         <version>3.2.1</version>
     </dependency>

     <dependency>
         <groupId>org.openoffice</groupId>
         <artifactId>juh</artifactId>
         <version>3.1.0</version>
     </dependency>

     <dependency>
         <groupId>org.openoffice</groupId>
         <artifactId>unoil</artifactId>
         <version>3.0.0</version>
     </dependency>

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>io.swagger</groupId>
         <artifactId>swagger-annotations</artifactId>
         <version>1.5.20</version>
     </dependency>
     <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
     </dependency>
     <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
     </dependency>
     <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
     <dependency>
         <groupId>org.artofsolving.jodconverter</groupId>
         <artifactId>jodconverter-core</artifactId>
         <version>3.0-beta-4</version>
     </dependency>

 </dependencies>

 <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
 </build>

需要注意的问题:
由于依赖版本原因转换不了docx文件。

到此这篇关于Java 使用openoffice进行word转换为pdf的方法步骤的文章就介绍到这了,更多相关Java openoffice word转换为pdf内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Mybatis中如何使用sum对字段求和

    Mybatis中如何使用sum对字段求和

    这篇文章主要介绍了Mybatis中如何使用sum对字段求和,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 解析Spring事件发布与监听机制

    解析Spring事件发布与监听机制

    本篇文章给大家介绍Spring事件发布与监听机制,通过 ApplicationEvent 事件类和 ApplicationListener 监听器接口,可以实现 ApplicationContext 事件发布与处理,需要的朋友参考下吧
    2021-06-06
  • SpringBoot前后端接口对接常见错误小结

    SpringBoot前后端接口对接常见错误小结

    SpringBoot前后端接口对接工作时,经常遇到请求500,400等问题,本文主要介绍了SpringBoot前后端接口对接常见错误小结,感兴趣的可以了解一下
    2022-01-01
  • springboot自定义stater启动流程

    springboot自定义stater启动流程

    这篇文章主要介绍了springboot自定义stater启动流程,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • 单点登录的三种方式和JWT的介绍与使用

    单点登录的三种方式和JWT的介绍与使用

    这篇文章主要说明了单点登录的三种方式和JWT的介绍与使用,加深自己的印象以及帮助的诸位小伙伴儿们,需要的朋友可以参考下
    2023-03-03
  • idea编写java程序详细图文步骤

    idea编写java程序详细图文步骤

    这篇文章主要给大家介绍了关于idea编写java程序的详细图文步骤,IDEA是用于Java语言开发的集成环境,它是业界公认的目前用于Java程序开发最好的工具,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • Springboot详细讲解循环依赖

    Springboot详细讲解循环依赖

    最近在使用Springboot做项目的时候,遇到了一个循环依赖的问题,所以下面这篇文章主要给大家介绍了关于springboot循环依赖实现以及分析的相关资料,需要的朋友可以参考下
    2022-06-06
  • Java使用Math.random()结合蒙特卡洛方法计算pi值示例

    Java使用Math.random()结合蒙特卡洛方法计算pi值示例

    这篇文章主要介绍了Java使用Math.random()结合蒙特卡洛方法计算pi值的方法,简单说明了结合具体实例蒙特卡洛方法的原理,并结合具体实例形式分析了java使用蒙特卡洛方法计算PI值的操作技巧,需要的朋友可以参考下
    2017-09-09
  • 使用Java实现验证码程序

    使用Java实现验证码程序

    这篇文章主要为大家详细介绍了使用Java实现验证码程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Java实现合并多个PDF的示例代码

    Java实现合并多个PDF的示例代码

    这篇文章主要介绍了通过Java实现合并多个PDF,并将合并后的新PDF存储到文件夹下,文中的示例代码简洁易懂,感兴趣的可以跟随小编一起试一试
    2022-01-01

最新评论