OpenOffice的使用说明

本文介绍如何使用OpenOffice将Office文档转换成PDF格式,并提供在线预览方案。涵盖安装步骤、文档格式转换方法及所需依赖库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenOffice的使用说明

1. 概述
1.1 Java程序中可以利用OpenOffice,将办公文档(包括doc、docx、xls、xlsx等)转换为PDF格式的文档。由于PDF格式的文档可以达到在线预览的效果,而Office办公文档实现在线预览相对来说会比较麻烦,但曲线救国,通过文档格式的转换,从而也很容易地达到对Office办公文档实现在线预览的效果。
2. OpenOffice的安装
2.1 以Windows版本的OpenOffice安装为例,到OpenOffice官网下载最新版本的OpenOffice软件【[OpenOffice下载地址](http://www.openoffice.org/download/index.html
2.2 下载完毕后进行安装,但需要记住软件的安装目录,如安装在D:/Program Files (x86)/OpenOffice 4,这个安装路径在文档格式转换的时候需要用到。
3. 文档格式转换
3.1 将Office文档转换为PDF格式的文档
    /**
     * 
     * @description:将Office格式的文档转换为PDF格式的文档
     * @author huang.j
     * @date 2017年5月24日 上午11:44:03
     * @param inputFilePath
     *
     */
    public void convertWord2Pdf(String inputFilePath) {

        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // OpenOffice安装在本地环境的目录
        String officeHome = "D:/Program Files (x86)/OpenOffice 4";
        config.setOfficeHome(officeHome);

        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();

        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        String outputFilePath = getReplaceFileAbsolutePath(inputFilePath, ".pdf");
        File inputFile = new File(inputFilePath);
        if (inputFile.exists()) {
            File outputFile = new File(outputFilePath);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            // 进行PDF格式的转换
            converter.convert(inputFile, outputFile);
        }

        officeManager.stop();
    }
3.2 更改文档后缀为指定的后缀名
    /**
     * 
     * @description:更改文档后缀为指定的后缀名
     * @author huang.j
     * @date 2017年5月24日 上午11:44:20
     * @param inputFilePath 输入文档的绝对路径
     * @param replaceEndWith 指定的后缀名
     * @return 返回替换指定后缀名的文档的绝对路径
     *
     */
    private String getReplaceFileAbsolutePath(String inputFilePath, String replaceEndWith) {

        String replaceFilePath = null;
        Pattern pattern = Pattern.compile("(\\.[a-zA-Z]+)");
        Matcher matcher = pattern.matcher(inputFilePath);
        String endWith = null;
        if(matcher.find()) {
            endWith = matcher.group(1);
        }

        if(StringUtil.isEmpty(endWith)) {
            return null;
        }

        replaceFilePath = inputFilePath.replaceAll(endWith, replaceEndWith);
        return replaceFilePath;
    }
4. PDF文档的预览
4.1 完成PDF格式的转换后,下一步就可以进行PDF文件的预览
    /**
     * 
     * @description:预览PDF文件
     * @author huang.j
     * @date 2017年5月23日 下午4:51:59
     * @param attathFile
     * @param response
     *
     */
    public void previewPdf(File attathFile, HttpServletResponse response) {

        response.setContentType("application/pdf");
        try {
            if(attathFile.exists()) {
                DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());
                DataInputStream dataInputStream = new DataInputStream(new FileInputStream(attathFile));

                byte[] buffer = new byte[2048];
                int len = buffer.length;
                while((len = dataInputStream.read(buffer, 0, len)) != -1) {
                    dataOutputStream.write(buffer);
                    dataOutputStream.flush();
                }

                dataInputStream.close();
                dataOutputStream.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
5. Office办公文档转换为PDF文档所需的jar包

1) commons-cli-xx.jar
2) commons-io-xx.jar
3) jodconverter-core-3.0-beta-4.jar
4) json-xxx.jar
5) juh-xx.jar
6) jurt-xx.jar
7) ridl-xx.jar
8) unoil-xx.jar

Jar包资源下载地址【点击连接】

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值