广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java 使用openoffice进行word转换为pdf的方法步骤
  • 421
分享到

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

2024-04-02 19:04:59 421人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

一、下载openoffice第三方工具 建议下载4.1.6版本 Http://www.openoffice.org/download/index.html 二、开启openoffi

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

--结束END--

本文标题: Java 使用openoffice进行word转换为pdf的方法步骤

本文链接: https://www.lsjlt.com/news/124980.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作