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 使用openoffice进行word转换为pdf的方法步骤

相关文章:

你感兴趣的文章:

标签云: