在线预览office技术总结(Office 转 PDF )

总结一下之前做的一个在线预览的office的技术。1、使用的技术:openOffice, jquery.media.js2、提前安装openOffice,3、看代码:public class Office2Pdf { // OpenOffice的安装目录,默认会安装到c盘下 private static String OpenOffice_HOME = "C:/

总结一下之前做的一个在线预览的office的技术。

1、使用的技术:openOffice, jquery.media.js

2、提前安装openOffice,

3、看代码:

public class Office2Pdf {    // OpenOffice的安装目录,默认会安装到c盘下    private static String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/program/";    // 启动服务的命令    private static String command = "soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";    private static Process process = null;    /      * 核心转pdf方法     *      * @param sourcefile 被转文件     * @param targetfile 转换后的文件     * @return     */    public static void  convertTo(File sourcefile, File targetfile)    {         try {             // 启动方法             if(process == null){                 process = startOpenOffice();             }             //8100就是启动openoffice的端口,             OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);                                   connection.connect();             //创建openoffice文档转换类             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);              //转换,传入源文件和目标文件;             converter.convert(sourcefile, targetfile);             //断开于openoffice服务的连接             connection.disconnect();         } catch (ConnectException e) {              e.printStackTrace();         }     }    /      * 启动openOffice服务     */    public static Process startOpenOffice(){         // 启动OpenOffice的服务的完整命令         String fullCommand = OpenOffice_HOME + command;         try {            return Runtime.getRuntime().exec(fullCommand);        } catch (IOException e) {            e.printStackTrace();        }         return null;    }}

关键字:office, pdf