在線預覽office技術總結(Office 轉 PDF )

總結一下以前作的一個在線預覽的office的技術。jquery

一、使用的技術:openOffice, jquery.media.jsless

二、提早安裝openOffice,socket

三、看代碼:code

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;
    }
}
相關文章
相關標籤/搜索