|
項目開發(fā)過程中,需求涉及到了各種文檔轉(zhuǎn)換為HTML或者網(wǎng)頁易顯示格式,現(xiàn)在將實現(xiàn)方式整理如下: 1
public class JacobUtil 2 { 3 public static final int WORD_HTML = 8; 4 5 public static final int WORD_TXT = 7; 6 7 public static final int EXCEL_HTML = 44; 8 9 /** 10 * WORD轉(zhuǎn)HTML 11 * @param docfile WORD文件全路徑 12 * @param htmlfile 轉(zhuǎn)換后HTML存放路徑 13 */ 14 public static void wordToHtml(String docfile, String htmlfile) 15 { 16 ActiveXComponent app = new ActiveXComponent("Word.Application"); // 啟動word 17 try 18 { 19 app.setProperty("Visible", new Variant(false)); 20 Dispatch docs = app.getProperty("Documents").toDispatch(); 21 Dispatch doc = Dispatch.invoke( 22 docs, 23 "Open", 24 Dispatch.Method, 25 new Object[] { docfile, new Variant(false), 26 new Variant(true) }, new int[1]).toDispatch(); 27 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { 28 htmlfile, new Variant(WORD_HTML) }, new int[1]); 29 Variant f = new Variant(false); 30 Dispatch.call(doc, "Close", f); 31 } 32 catch (Exception e) 33 { 34 e.printStackTrace(); 35 } 36 finally 37 { 38 app.invoke("Quit", new Variant[] {}); 39 } 40 } 41 42 /** 43 * EXCEL轉(zhuǎn)HTML 44 * @param xlsfile EXCEL文件全路徑 45 * @param htmlfile 轉(zhuǎn)換后HTML存放路徑 46 */ 47 public static void excelToHtml(String xlsfile, String htmlfile) 48 { 49 ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 啟動word 50 try 51 { 52 app.setProperty("Visible", new Variant(false)); 53 Dispatch excels = app.getProperty("Workbooks").toDispatch(); 54 Dispatch excel = Dispatch.invoke( 55 excels, 56 "Open", 57 Dispatch.Method, 58 new Object[] { xlsfile, new Variant(false), 59 new Variant(true) }, new int[1]).toDispatch(); 60 Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] { 61 htmlfile, new Variant(EXCEL_HTML) }, new int[1]); 62 Variant f = new Variant(false); 63 Dispatch.call(excel, "Close", f); 64 } 65 catch (Exception e) 66 { 67 e.printStackTrace(); 68 } 69 finally 70 { 71 app.invoke("Quit", new Variant[] {}); 72 } 73 } 74 75 } 76 ![]() 當時我在找轉(zhuǎn)換控件時,發(fā)現(xiàn)網(wǎng)易也轉(zhuǎn)載了一偏關(guān)于Jacob使用幫助,但其中出現(xiàn)了比較嚴重的錯誤:String htmlfile = "C:\\AA"; 只指定到了文件夾一級,正確寫法是String htmlfile = "C:\\AA\\xxx.html";
到此WORD/EXCEL轉(zhuǎn)換HTML就已經(jīng)差不多了,相信大家應(yīng)該很清楚了:)
二、使用XPDF將PDF轉(zhuǎn)換為HTML
1、下載xpdf最新版本,地址:http://www./xpdf/download.html
2、下載中文支持包 我下載的是xpdf-chinese-simplified.tar.gz
3、下載pdftohtml支持包 地址:http:///projects/pdftohtml/ 我下載的是:pdftohtml-0.39-win32.tar.gz
4、解壓調(diào)試 1) 先將xpdf-3.02pl2-win32.zip解壓,解壓后的內(nèi)容可根據(jù)需要進行刪減,如果只需要轉(zhuǎn)換為txt格式,其他的exe文件可以刪除,只保留pdftotext.exe,以此類推; 2) 然后將xpdf-chinese-simplified.tar.gz解壓到剛才xpdf-3.02pl2-win32.zip的解壓目錄; 3) 將pdftohtml-0.39-win32.tar.gz解壓,pdftohtml.exe解壓到xpdf-3.02pl2-win32.zip的解壓目錄; 4) 目錄結(jié)構(gòu): +---[X:\xpdf] |-------各種轉(zhuǎn)換用到的exe文件 | |-------xpdfrc | +------[X:\xpdf\xpdf-chinese-simplified] | | +-------很多轉(zhuǎn)換時需要用到的字符文件
xpdfrc:此文件是用來聲明轉(zhuǎn)換字符集對應(yīng)路徑的文件
5) 修改xpdfrc文件(文件原名為sample-xpdfrc) 修改文件內(nèi)容為: Txt代碼 ![]() #----- begin Chinese Simplified support package cidToUnicode Adobe-GB1 xpdf-chinese-simplified\Adobe-GB1.cidToUnicode unicodeMap ISO-2022-CN xpdf-chinese-simplified\ISO-2022-CN.unicodeMap unicodeMap EUC-CN xpdf-chinese-simplified\EUC-CN.unicodeMap unicodeMap GBK xpdf-chinese-simplified\GBK.unicodeMap cMapDir Adobe-GB1 xpdf-chinese-simplified\CMap toUnicodeDir xpdf-chinese-simplified\CMap fontDir C:\WINDOWS\Fonts displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf #----- end Chinese Simplified support package ![]() 6) 創(chuàng)建bat文件pdftohtml.bat(放置的路徑不能包含空格) 內(nèi)容為: Txt代碼 ![]() @echo off set folderPath=%1 set filePath=%2 cd /d %folderPath% pdftohtml -enc GBK %filePath% exit JAVA代碼 public class ConvertPdf { private static String INPUT_PATH; private static String PROJECT_PATH; public static void convertToHtml(String file, String project) { INPUT_PATH = file; PROJECT_PATH = project; if(checkContentType()==0) { toHtml(); } } private static int checkContentType() { String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length()) .toLowerCase(); if (type.equals("pdf")) return 0; else return 9; } private static void toHtml() { if(new File(INPUT_PATH).isFile()) { try { String cmd = "cmd /c start X:\\pdftohtml.bat \"" + PROJECT_PATH + "\" \"" + INPUT_PATH + "\""; Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } } } } ![]() String cmd = "....";此處代碼是調(diào)用創(chuàng)建的bat文件進行轉(zhuǎn)換
8) 測試轉(zhuǎn)換 public static void main(String[] args) { ConvertPdf.convertToHtml("C:\\test.pdf", "X:\\xpdf"); } ![]() |
|
|