Java用OpenOffice将word转换为PDF并预览

本文在原文的基础上有所修改,原文请参考:

http://titanseason.iteye.com/blog/1471606 由于此blog不支持附件附件请到此处下载

http://my.oschina.NET/bigyuan/blog/165464

1. 需要用的软件

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

    JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载

    jra包 需要jodconverter-2.2.2,jodconverter-2.2.1只能转.doc .xls .ppt

2.启动OpenOffice的服务

 

安装完openoffice,安装服务

cd C:\Program Files (x86)\OpenOffice 4\program

执行

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

查看是否安装成功,查看端口对应的pid

    netstat -ano|findstr "8100"

 查看pid对应的服务程序名

    tasklist|findstr "pid值"

 

 

3.将JodConverter相关的jar包添加到项目中   

  

4. 下面是实现代码

附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行

public static int office2PDF(String sourceFile, String destFile) throws FileNotFoundException {
                try {
                         File inputFile = new File(sourceFile);
                         if (!inputFile.exists()) {
                             logger.info("找不到源文件");
                             return -1;// 找不到源文件, 则返回-1
                         }
                    // 如果目标路径不存在, 则新建该路径
                         File outputFile = new File(destFile);
                        if (!outputFile.getParentFile().exists()) {
                                outputFile.getParentFile().mkdirs();
                               
                        }
                         // connect to an OpenOffice.org instance running on port 8100
                         OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                                      "127.0.0.1", 8100);
                         connection.connect();
                         // convert
                        DocumentConverter converter = new OpenOfficeDocumentConverter(
                                     connection);
                         converter.convert(inputFile, outputFile);
                                    // close the connection
                        connection.disconnect();

                         return 0;
                     } catch (ConnectException e) {
                        e.printStackTrace();
                     } catch (IOException e) {
                         e.printStackTrace();
                    }

                 return 1;
             }


H5使用<embed src="xxx.pdf" />标签就可以在页面预览

maven 地址 

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>jurt</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>ridl</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>juh</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>unoil</artifactId>
    <version>3.0.1</version>
</dependency>
jodconverter2.2.2 没有,只能手动下载配置

原文:http://www.cnblogs.com/cxxjohnson/p/6880545.html


猜你喜欢

转载自blog.csdn.net/muumo/article/details/78395016