ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java使用Openoffice将word、ppt转换为PDF

2021-01-14 17:33:16  阅读:268  来源: 互联网

标签:word extension format org return Openoffice new PDF public


项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览。

OpenOffice

OpenOffice.org 是一套跨平台的办公室软件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用、及推广它。

下载地址

http://www.openoffice.org/

JodConverter

jodconverter-2.2.2.zip 下载地址:

http://sourceforge.net/projects/jodconverter/files/JODConverter/

Word转换

启动OpenOffice的服务

进入openoffice安装目录,通过cmd启动一个soffice服务,启动的命令是soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

如果觉得后台运行OpenOffice服务比较麻烦,可以通过运行代码

public class PDFDemo {

    public static boolean officeToPDF(String sourceFile, String destFile) {
        try {

            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                // 找不到源文件, 则返回false
                return false;
            }//需要获取资料的朋友请加Q君样:290194256*
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            //如果目标文件存在,则删除
            if (outputFile.exists()) {
                outputFile.delete();
            }
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            //用于测试openOffice连接时间
            System.out.println("连接时间:" + df.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);
            //测试word转PDF的转换时间
            System.out.println("转换时间:" + df.format(new Date()));
            connection.disconnect();
            return true;
        } catch (ConnectException e) {
            e.printStackTrace();
            System.err.println("openOffice连接失败!请检查IP,端口");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }//需要获取资料的朋友请加Q君样:290194256*

    public static void main(String[] args) {
        officeToPDF("E:\\test.docx", "E:\\test.pdf");
    }
}

Word、ppt转Html

只需要将后缀名从.pdf改为.html即可。

public static void main(String[] args) {
    officeToPDF("E:\\test.docx", "E:\\test.html");
}

Maven配置

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>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-jdk14</artifactId>
	<version>1.4.3</version>
</dependency>

Maven只有 2.2.1版本,2.2.1版本有一个问题,那就是不兼容docx和pptx,如果你们不使用jodconverter-2.2.2 中lib,而想要使用2.2.1版本,需要修改一下 BasicDocumentFormatRegistry 类中的 getFormatByFileExtension方法:

新建包 com.artofsolving.jodconverter
新建类BasicDocumentFormatRegistry,复制下面代码

package com.artofsolving.jodconverter;

/**
 * @author 李文浩
 * @date 2017/12/25
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
    private List documentFormats = new ArrayList();

    public BasicDocumentFormatRegistry() {
    }

    public void addDocumentFormat(DocumentFormat documentFormat) {
        this.documentFormats.add(documentFormat);
    }

    protected List getDocumentFormats() {
        return this.documentFormats;
    }//需要获取资料的朋友请加Q君样:290194256*

    public DocumentFormat getFormatByFileExtension(String extension) {
        if (extension == null) {
            return null;
        } else {
            if (extension.indexOf("doc") >= 0) {
                extension = "doc";
            }
            if (extension.indexOf("ppt") >= 0) {
                extension = "ppt";
            }
            if (extension.indexOf("xls") >= 0) {
                extension = "xls";
            }
            String lowerExtension = extension.toLowerCase();
            Iterator it = this.documentFormats.iterator();

            DocumentFormat format;
            do {
                if (!it.hasNext()) {
                    return null;
                }

                format = (DocumentFormat)it.next();
            } while(!format.getFileExtension().equals(lowerExtension));

            return format;
        }
    }

    public DocumentFormat getFormatByMimeType(String mimeType) {
        Iterator it = this.documentFormats.iterator();

        DocumentFormat format;
        do {
            if (!it.hasNext()) {
                return null;
            }
//需要获取资料的朋友请加Q君样:290194256*

            format = (DocumentFormat)it.next();
        } while(!format.getMimeType().equals(mimeType));

        return format;
    }
}

下面是增加的部分,仅仅增加了将docx按照doc的处理方式处理。而2.2.2版本已经默认增加了。

if (extension.indexOf("doc") >= 0) {
    extension = "doc";
}
//需要获取资料的朋友请加Q君样:290194256*
if (extension.indexOf("ppt") >= 0) {
    extension = "ppt";
}
if (extension.indexOf("xls") >= 0) {
    extension = "xls";
}

在这里插入图片描述
在这里插入图片描述
最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:290194256*

标签:word,extension,format,org,return,Openoffice,new,PDF,public
来源: https://blog.csdn.net/weixin_53341657/article/details/112620924

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有