java调用openoffice将office系列文档转换为PDF的示例方法

  在上一篇说到openoffice将msoffice转成pdf的时候会存在排版错位的问题,或者有的内容消失了,这是因为msoffice中的一些特有格式,openoffice不识别解析不了导致的。当然大部分的普通msoffice文档转换成pdf时,效果还是令人满意的。

  所以这次说一下怎么用openoffice完成msoffice到pdf的转换。

 

PS:本文说的是java如何调用本机(当前服务器)的openoffice服务进行文档转换;调用其他服务器上的openoffice进行文档转换下一次再说。

 

1.前提条件

  本机测试,本机安装一个openoffice软件即可。

  若是被部署项目的服务器,可以在服务器本地安装一个openoffice软件;也可以在其他服调用其他服务器上的openoffice服务进行文档转换。

 

  openoffice的下载地址:http://www.openoffice.org/

  我本机的openoffice软件安装目录:C:\Program Files (x86)\OpenOffice 4 ,如下图所示:

2.创建项目,引入jar包

  1) maven项目。我本地的pom.xml配置如下:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>indi.johnny</groupId>
    <artifactId>openoffice-convert</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>


        <dependency>
        <groupId>com.github.livesense</groupId>
        <artifactId>jodconverter-core</artifactId>
        <version>1.0.5</version>
        </dependency>
    </dependencies>
</project>

2) 普通java项目 将下图中的jar包从这个地址下载下来引入项目即可: http://www.mvnrepository.com/artifact/com.github.livesense/jodconverter-core/1.0.5 ; guava包也可以在  http://www.mvnrepository.com搜到,下载引入项目即可。

  大概的jar包如图所示:

 

 

3.上代码

package indi.johnny.convert;

import java.io.File;
import java.io.FileNotFoundException;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;


//转换文档为pdf
public class OpenOfficePdfConvert {

    /**
     * @param args
     */
    private static OfficeManager officeManager;
    private static String OFFICE_HOME = "D:/software/OpenOffice 4/";
    private static int port[] = { 8100 };
    
    public void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException {
       
        startService();
        System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);
        
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(new File(inputFile), new File(outputFile));
        
        stopService();
        System.out.println();

    }

    // 打开服务器
    public static void startService() {
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        try {
            System.out.println("准备启动服务....");
            configuration.setOfficeHome(OFFICE_HOME);// 设置OpenOffice.org安装目录
            configuration.setPortNumbers(port); // 设置转换端口,默认为8100
            configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时

            officeManager = configuration.buildOfficeManager();
            officeManager.start(); // 启动服务
            System.out.println("office转换服务启动成功!");
        } catch (Exception ce) {
            System.out.println("office转换服务启动失败!详细信息:" + ce);
        }
    }

    // 关闭服务器
    public static void stopService() {
        System.out.println("关闭office转换服务....");
        if (officeManager != null) {
            officeManager.stop();
        }
        System.out.println("关闭office转换成功!");
    }

    public static void main(String[] args) throws Exception {
        String path = "C:/Users/johnny/Desktop/文档/20170420/test/001/";
        OpenOfficePdfConvert opc = new OpenOfficePdfConvert();
        opc.convert2PDF(path+"1.docx", path+"1.pdf");
    }

}

将代码中的  OFFICE_HOME换成自己的openoffice的安装路径,端口8100不用动。

 找一个msoffice文档测试运行一下,应该是可以的。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值