Java:OpenOffice方式實現Word轉pdf/html/htm

本來的想法想要直接在頁面上實現預覽,包括預覽樣式等等,相似這位博主這種方式:html

http://blog.csdn.net/lbf5210/article/details/50519190  可是發現其中的flowpaper貌似只有exe文件可下載,另外就是安裝太多插件,麻煩。因此想了下,只要生成文件就是了,不採用其中的預覽插件,這樣就簡單得多了。但必需要安裝OpenOffice服務,方可實現轉換。java

思路:傳入已doc/docx文件流對象(無論後綴很方便,一次性處理) -> OpenOfficeConnection創建鏈接 -> DocumentConverter對象實現轉換 -> 關閉OpenOfficeConnection鏈接。工具

其實實現過程也很簡單,我這裏寫的工具類能夠根據傳入的文檔的後綴及想要生成的文件格式來自動處理,post

其實關鍵代碼就幾行而已,以下:.net

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(docInputFile, htmlOutputFile);
            connection.disconnect();

其餘都是細節問題,一樣貼出完整代碼demo,看註釋便可:插件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;

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 OfficeParseUtil {
	
	private static OfficeParseUtil officeParseUtil;
	
	/**
	 * 描述:實例化
	 * @return
	 * OfficeParseUtil
	 */
	public static synchronized OfficeParseUtil getOfficeParseUtil(){
		 if (officeParseUtil == null) {
			 officeParseUtil = new OfficeParseUtil();
	        }
	        return officeParseUtil;
	}
	
	/**
	 * 描述:轉換office文件
	 * @param fromFileInputStream 文件
	 * @param toFilePath  保存地址
	 * @param fileName  文件完整名稱,帶後綴(如:doc docx xls ppt)
	 * @param type 轉換類型,如:pdf html htm
	 * @return
	 * @throws IOException
	 * String
	 */
    public String parseOffice(InputStream fromFileInputStream, String toFilePath,String fileName,String type) throws IOException {
        String timesuffix = fileName.substring(1,fileName.indexOf(".")-1);//截取相同相同文件名
        String postfix = fileName.substring(fileName.indexOf(".")+1);//截取文件後綴
        String docFileName = null;
        String htmFileName = null;
        if("doc".equals(postfix)){
            docFileName = "doc_" + timesuffix + ".doc";
            htmFileName = "doc_" + timesuffix + "."+type;
        }else if("docx".equals(postfix)){
            docFileName = "docx_" + timesuffix + ".docx";
            htmFileName = "docx_" + timesuffix + "."+type;
        }else if("xls".equals(postfix)){
            docFileName = "xls_" + timesuffix + ".xls";
            htmFileName = "xls_" + timesuffix + "."+type;
        }else if("ppt".equals(postfix)){
            docFileName = "ppt_" + timesuffix + ".ppt";
            htmFileName = "ppt_" + timesuffix + "."+type;
        }else{
            return null;
        }

        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
        if (!new File(toFilePath).exists()) {
        	docInputFile.getParentFile().mkdirs();
		}
        if (htmlOutputFile.exists())
            htmlOutputFile.delete();
        htmlOutputFile.createNewFile();
        if (docInputFile.exists())
            docInputFile.delete();
        docInputFile.createNewFile();
        /**
         * 由fromFileInputStream構建輸入文件
         */
        try {
            OutputStream os = new FileOutputStream(docInputFile);
            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            os.close();
            fromFileInputStream.close();
        } catch (IOException e) {
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
            // convert
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(docInputFile, htmlOutputFile);
            connection.disconnect();
        } catch (ConnectException e) {
        	htmFileName = null;
        	//轉換出錯刪除臨時文件
        	htmlOutputFile.delete();
            System.err.println("文件轉換出錯,請檢查OpenOffice服務是否啓動。");
        }finally{
        	// 轉換完以後刪除word文件
            docInputFile.delete();
        }
        return htmFileName;
    }
    
    public static void main(String[] args) throws IOException  {
    	OfficeParseUtil officeParseUtil = new OfficeParseUtil();
        File file = null;
        FileInputStream fileInputStream = null;

        file = new File("F:/wordtest/xxxxxx.doc");
        fileInputStream = new FileInputStream(file);
//      coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
        officeParseUtil.parseOffice(fileInputStream, "F:/wordtest/pdf/","白蟻防治協議.doc","pdf");

    }

}

注意:code

一、啓動OpenOffice服務服務。htm

二、這裏不侷限轉換成pdf,也可轉換成html 、 htm頁面對象

三、轉換後html/htm 的樣式沒有pdf的效果好,例如協議文件最後的簽名存在排版有錯亂blog

四、對文檔中的Tab縮進的樣式兼容也並很差

五、「1、 2、」使用這種Office自帶樣式標題會變成「一、 二、」

感謝 http://blog.csdn.net/yjclsx/article/details/51445546

相關文章
相關標籤/搜索