首页 > 代码库 > 利用LibreOffice转换ppt、doc转化pdf

利用LibreOffice转换ppt、doc转化pdf

利用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