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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Java之Spring Bean 作用域和生命周期

    Java之Spring Bean 作用域和生命周期

    这篇文章主要介绍了Java Bean的作用域和生命周期,Bean 的作用域是指 Bean 在 Spring 整个框架中的某种行为模式,所谓的⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程,我们把这个过程就叫做⼀个对象的⽣命周期,感兴趣的同学可以参考阅读
    2023-04-04
  • SpringBoot+MinIO实现对象存储方式

    SpringBoot+MinIO实现对象存储方式

    这篇文章主要介绍了SpringBoot+MinIO实现对象存储方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • java ant 配置及构建项目图文教程

    java ant 配置及构建项目图文教程

    以下是对java ant配置及构建项目进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-08-08
  • SpringBoot 内嵌 camunda的配置方法

    SpringBoot 内嵌 camunda的配置方法

    Camunda是一个基于Java的框架,支持用于工作流和流程自动化的BPMN、用于案例管理的CMMN和用于业务决策管理的DMN,这篇文章主要介绍了SpringBoot 内嵌 camunda,需要的朋友可以参考下
    2024-06-06
  • Java详细分析讲解自动装箱自动拆箱与Integer缓存的使用

    Java详细分析讲解自动装箱自动拆箱与Integer缓存的使用

    装箱就是把基本类型转换成包装类,拆箱就是把包装类转换成基本类型,下面这篇文章主要给大家介绍Java中自动装箱、自动拆箱与Integer缓存,需要的朋友可以参考下
    2022-04-04
  • Java Pattern和Matcher字符匹配方式

    Java Pattern和Matcher字符匹配方式

    这篇文章主要介绍了Java Pattern和Matcher字符匹配方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • java cpu飙升问题的详细分析和处理方法

    java cpu飙升问题的详细分析和处理方法

    Java中CPU占用过高是一个常见的问题,可能是由于线程过多、死循环、长时间的阻塞、死锁、GC频繁等原因导致的,这篇文章主要介绍了java cpu飙升问题的详细分析和处理方法,需要的朋友可以参考下
    2025-03-03
  • Spring Boot 实例化bean如何选择代理方式

    Spring Boot 实例化bean如何选择代理方式

    这篇文章主要为大家介绍了Spring Boot实例化bean如何选择代理方式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Java实现滑动窗口算法的示例代码

    Java实现滑动窗口算法的示例代码

    滑动窗口算法是一种高效解决子数组、子字符串问题的算法,广泛应用于数据流处理、网络限流和字符串操作等场景,本文将详细解析滑动窗口算法的核心思想、常见问题及其实现方式,需要的朋友可以参考下
    2025-03-03
  • Java接口请求重试机制的几种常见方法

    Java接口请求重试机制的几种常见方法

    Java接口请求重试机制是保证系统稳定性和容错能力的重要手段之一,当接口请求发生失败或暂时性错误时,通过重试机制可以提高请求的成功率,本文将详细介绍Java接口请求重试机制的几种常见方法,需要的朋友可以参考下
    2023-11-11

最新评论