利用LibreOffice转换ppt、doc转化pdf

LibreOffice下载地址:  http://www.libreoffice.org/download/libreoffice-fresh/

jodconverter jar包下载:http://sourceforge.net/projects/jodconverter/files/JODConverter/

import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.ConnectException;
    import java.util.Date;  

    import com.artofsolving.jodconverter.DocumentConverter;
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  

    public class Office2PDF {  

        public static int office2PDF(String sourceFile, String destFile) {  

            OpenOfficeConnection connection = null;
            try {
                File inputFile = new File(sourceFile);
                if (!inputFile.exists()) {
                    return -1;//文件不存在
                }  

                //文件夹不存在创建目录
                File outputFile = new File(destFile);
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }  

                // connect to an OpenOffice.org instance running on port 8100
                connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                connection.connect();  

                // convert
                DocumentConverter converter = new OpenOfficeDocumentConverter(
                        connection);
                converter.convert(inputFile, outputFile);  

                return 0;  

            } catch (ConnectException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(connection!=null){
                    // close the connection
                    connection.disconnect();
                }
            }  

            return 1;
        }  

        public static void main(String[] args){
            Date startDate = new Date();
            String sourceFile = "d:\\code\\alipay.ppt";
            String destFile = "d:\\code\\change2.pdf";
            System.out.println(Office2PDF.office2PDF(sourceFile, destFile));
            Date endDate = new Date();
            System.out.println("the cost time is "+(endDate.getTime()-startDate.getTime()));
        }
    }      

先运行

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

soffice 在LibreOffice安装目录下

如果出现下面的异常

com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document

尽量使用最新的LibreOffice

利用LibreOffice转换ppt、doc转化pdf的更多相关文章

  1. Python实现doc转化pdf

    Python实现doc转化pdf python源码实现doc转化pdf #-*- coding:utf-8 -*- # doc2pdf.py: python script to convert doc ...

  2. libreoffice转换文件为pdf文件乱码问题解决办法

    最近系统需要一个office文件预览功能 解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件 最后在前台使用flexpaper ...

  3. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

  4. Java使用Jacob将Word、Excel、PPT转化成PDF

    使用Jacob将金山WPS转化成PDF,其中WPS文字使用KWPS.Aplication.Excel表格是KET.Application.演示文档是KWPP.Application,废话不多说,直接上 ...

  5. 【强大的PDF格式转换工具】Lighten PDF Converter OCR for Mac 6.2.0

    [简介] Lighten PDF Converter OCR 是一款Mac上强大的PDF格式转换工具,可以将PDF文档快速批量的转换为Office (Word, Excel, PowerPoint), ...

  6. 使用python调用wps v9转换office文件到pdf

    #!/usr/bin/python2.6 # -*- coding: utf-8 -*- # pip install timeout-decorator import os import win32c ...

  7. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  8. 利用Swig转换C++代码为C#可用的代码

    详细的文件路径为:http://user.qzone.qq.com/1259374136/blog/1432887689 Swig学习教程 1.Swig的基本介绍 SWIG(Simplified Wr ...

  9. [错误解决] Libreoffice转换不成功,直接不做任何操作

    问题描述: Libreoffice在版本5.3.0之前都存在这个问题.现象是:当你运行其中一个LibreOffice的时候,再运行另外一个Libreoffice转换时,将不做任何操作. 解决方案: 如 ...

随机推荐

  1. LINQ to DataSet的DataTable操作

    1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...

  2. ZoneMinder配置与使用

    ZoneMinder是一套基于Linux操作系统的摄像机的视像数据监控的应用软件.应用范围广泛,包括商业或家居防盗等.ZoneMinder支持单一或多台视像镜头应用,包括摄取.分析.记录.和监视来源, ...

  3. 【Spring 1】Spring简介

    一.Spring简介 首先,Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开 ...

  4. java 进制相互转换

    public class test{ public static void main(String[]args){ //十进制转二进制. public static void toBin(int nu ...

  5. 实现IHttpModule接口,给每个页面输出一段脚本

    在App_Code文件中添加TGModule.cs using System; using System.Data; using System.Configuration; using System. ...

  6. Objective-C中的self和super

    1.有过面向对象的人知道,self相当于this,super相当于调用父类的方法 2.self是类的隐藏的参数,指向当前调用方法的类,另一个隐藏参数是_cmd,代表当前类方法的selector. su ...

  7. [leetcode]_Maximum Depth of Binary Tree

    第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 ...

  8. javascript oo实现(转)

    javascript oo实现 By purplebamboo 7月 13 2014 更新日期:8月 21 2014 文章目录 1. 原始时代最简单的oo实现 2. 石器时代的oo实现 3. 工业时代 ...

  9. 在c#中使用mongo-csharp-driver操作mongodb

    1)下载安装 下载地址:http://github.com/mongodb/mongo-csharp-driver/downloads 编译之后得到两个dll MongoDB.Driver.dll:顾 ...

  10. 什么是Ajax无刷新技术?

    浏览器实例化一个Ajax对象,这个对象发送一个HTTP请求,并且携带一定的参数,传输到后台.后台服务器接收这些参数,同时过滤一下传过来的参数,做出逻辑判断.如果需要数据库操作参与,就要取出数据,格式化 ...