文档格式转换--JodConverter

功能介绍

JodConverter通过OpenOffice实现对各种类型文件的转换,支持的文档类型如下表:

源文件格式目标文件格式
Text Formats
OpenDocument Text (*.odt)Portable Document Format (*.pdf)
OpenOffice.org 1.0 Text (*.sxw)OpenDocument Text (*.odt)
Rich Text Format (*.rtf)OpenOffice.org 1.0 Text (*.sxw)
Microsoft Word (.doc、.docx)Rich Text Format (*.rtf)
WordPerfect (*.wpd)Microsoft Word (*.doc)
Plain Text (*.txt)Plain Text (*.txt)
HTML(*.html)HTML2 (*.html)
-MediaWiki wikitext (*.wiki)
Spreadsheet Formats
OpenDocument Spreadsheet (*.ods)Portable Document Format (*.pdf)
OpenOffice.org 1.0 Spreadsheet (*.sxc)OpenDocument Spreadsheet (*.ods)
Microsoft Excel (*.xls)OpenOffice.org 1.0 Spreadsheet (*.sxc)
Comma-Separated Values (*.csv)Microsoft Excel (*.xls)
Tab-Separated Values (*.tsv)Comma-Separated Values (*.csv)
-Tab-Separated Values (*.tsv)
-HTML(*.html)
Presentation Formats
OpenDocument Presentation (*.odp)Portable Document Format (*.pdf)
OpenOffice.org 1.0 Presentation (*.sxi)Macromedia Flash (*.swf)
Microsoft PowerPoint (*.ppt)OpenDocument Presentation (*.odp)
-OpenOffice.org 1.0 Presentation (*.sxi)
-Microsoft PowerPoint (*.ppt)
-HTML (*.html)
Drawing Formats
OpenDocument Drawing (*.odg)Scalable Vector Graphics (*.svg)
-Macromedia Flash (*.swf)

1 源文件格式中的HTML进行转换时可能不像浏览器展示那么准确,只能转换打印友好的网页

2 目标文件格式中的HTML可能包含多个文件,其他格式只会产生一个文件

官方网址:http://www.artofsolving.com/opensource/jodconverter/guide.html

应用案例

部署OpenOffice

使用OpenOffice3版本,安装OpenOffice完成后。在OpenOffice目录下使用以下命令启动OpenOffice,其中port指定了OpenOffice服务的端口,需与开发连接端口一致;nofirststartwizard指定不打开OpenOffice启动窗口,防止OpenOffice无法正常提供服务。

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

代码开发

采用JodConverter2.2.2版本开发

OpenOfficeService类用于和OpenOffice建立连接,考虑到JodConverter与OpenOffice建立连接较慢,OpenOfficeService类采用单例模式,提供了start和stop方法用于开启和关闭OpenOffice连接。


import java.io.IOException;

import org.apache.log4j.Logger;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

/**
 * ClassName: OpenOfficeService <br>
 * Description: OpenOffice服务,支持OpenOffice3.*版本,不支持OpenOffice4.*<br>
 * Create Date: 2017年1月11日 上午9:20:25 <br>
 * Modified By: <br>
 * Modified Date: <br>
 * Modified Content: <br>
 * Version: 1.0 <br>
 */
public class OpenOfficeService {

    Logger log = Logger.getLogger(OpenOfficeService.class);

    /**
     * OpenOffice服务单例
     */
    private volatile static OpenOfficeService instance = new OpenOfficeService();

    /**
     * 获取 OpenOffice服务单例<br>
     * @return OpenOffice服务单例
     */
    public static OpenOfficeService getInstance() {
        return instance;
    }

    /**
     * 私有构造函数,通过静态方法getInstance获取实例对象
     */
    private OpenOfficeService() {

    }

    /**
     * OpenOffice端口
     */
    private int OPEN_OFFICE_PORT = 8100;

    /**
     * OpenOffice安装目录
     */
    private String Open_Office_HOME = "C:/Program Files (x86)/OpenOffice.org 3/";

    /**
     * OpenOffice连接
     */
    private OpenOfficeConnection connection;

    /**
     * 获取 OpenOffice连接<br>
     * @return connection OpenOffice连接
     * @throws IOException 
     */
    public OpenOfficeConnection getConnection() throws IOException {
        if(!isRunning()) {
            start();
        }
        return connection;
    }

    /**
     * OpenOffice进程
     */
    private Process openOfficeProcess;

    /**
     * 是否处于运行状态<br>
     * @return 运行状态
     */
    public boolean isRunning() {
        if(connection == null) {
            return false;
        }
        return connection.isConnected();
    }

    /**
     * Description: 启动服务<br>
     * Create By: XX <br>
     * Create Date: 2017年1月11日 上午9:27:47
     * @return 是否成功
     * @throws IOException
     */
    public void start() throws IOException {
        log.info("开始启动OpenOffice服务");
        /*String command = Open_Office_HOME + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port="
                + OPEN_OFFICE_PORT + ";urp;\"";
        openOfficeProcess = Runtime.getRuntime().exec(command);*/
        log.info("开始连接到OpenOffice服务");
        connection = new SocketOpenOfficeConnection(OPEN_OFFICE_PORT);
        connection.connect();
    }

    /**
     * Description: 停止服务<br>
     * Create By: XX <br>
     * Create Date: 2017年1月11日 上午9:29:42
     */
    public void stop() {
        log.info("关闭OpenOffice服务");
        if(connection != null && connection.isConnected()) {
            connection.disconnect();
        }
        if(openOfficeProcess != null) {
            openOfficeProcess.destroy();
        }
    }

}

转换文件格式

    OpenOfficeConnection connection;
    try {
        connection = OpenOfficeService.getInstance().getConnection();
    } catch (ConnectException e) {
        throw new Exception("无法连接到OpenOffice", e);
    }
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    // sourceFile源文件
    // destFile目标文件
    converter.convert(sourceFile, destFile);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值