項目開發(fā)過程中,需求涉及到了各種文檔轉(zhuǎn)換為HTML或者網(wǎng)頁易顯示格式,現(xiàn)在將實現(xiàn)方式整理如下:
一、使用Jacob轉(zhuǎn)換Word,Excel為HTML
“JACOB一個Java-COM中間件.通過這個組件你可以在Java應(yīng)用程序中調(diào)用COM組件和Win32 libraries。”
首先下載Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未測試),與先前的Jacob1.7差別不大
1、將壓縮包解壓后,Jacob.jar添加到Libraries中;
2、將Jacob.dll放至“WINDOWS\SYSTEM32”下面。
需要注意的是:
【使用IDE啟動Web服務(wù)器時,系統(tǒng)讀取不到Jacob.dll,例如用MyEclipse啟動Tomcat,就需要將dll文件copy到MyEclipse安裝目錄的“jre\bin”下面。
一般系統(tǒng)沒有加載到Jacob.dll文件時,報錯信息為:“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】
新建類:
- public class JacobUtil
- {
- public static final int WORD_HTML = 8;
-
- public static final int WORD_TXT = 7;
-
- public static final int EXCEL_HTML = 44;
-
-
-
-
-
-
- public static void wordToHtml(String docfile, String htmlfile)
- {
- ActiveXComponent app = new ActiveXComponent("Word.Application");
- try
- {
- app.setProperty("Visible", new Variant(false));
- Dispatch docs = app.getProperty("Documents").toDispatch();
- Dispatch doc = Dispatch.invoke(
- docs,
- "Open",
- Dispatch.Method,
- new Object[] { docfile, new Variant(false),
- new Variant(true) }, new int[1]).toDispatch();
- Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
- htmlfile, new Variant(WORD_HTML) }, new int[1]);
- Variant f = new Variant(false);
- Dispatch.call(doc, "Close", f);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- app.invoke("Quit", new Variant[] {});
- }
- }
-
-
-
-
-
-
- public static void excelToHtml(String xlsfile, String htmlfile)
- {
- ActiveXComponent app = new ActiveXComponent("Excel.Application");
- try
- {
- app.setProperty("Visible", new Variant(false));
- Dispatch excels = app.getProperty("Workbooks").toDispatch();
- Dispatch excel = Dispatch.invoke(
- excels,
- "Open",
- Dispatch.Method,
- new Object[] { xlsfile, new Variant(false),
- new Variant(true) }, new int[1]).toDispatch();
- Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
- htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
- Variant f = new Variant(false);
- Dispatch.call(excel, "Close", f);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- app.invoke("Quit", new Variant[] {});
- }
- }
-
- }
public class JacobUtil
{
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
/**
* WORD轉(zhuǎn)HTML
* @param docfile WORD文件全路徑
* @param htmlfile 轉(zhuǎn)換后HTML存放路徑
*/
public static void wordToHtml(String docfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 啟動word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
/**
* EXCEL轉(zhuǎn)HTML
* @param xlsfile EXCEL文件全路徑
* @param htmlfile 轉(zhuǎn)換后HTML存放路徑
*/
public static void excelToHtml(String xlsfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 啟動word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
}
當(dāng)時我在找轉(zhuǎn)換控件時,發(fā)現(xiàn)網(wǎng)易也轉(zhuǎn)載了一偏關(guān)于Jacob使用幫助,但其中出現(xiàn)了比較嚴(yá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.foolabs.com/xpdf/download.html
我下載的是xpdf-3.02pl2-win32.zip
2、下載中文支持包
我下載的是xpdf-chinese-simplified.tar.gz
3、下載pdftohtml支持包
地址:http://sourceforge.net/projects/pdftohtml/
我下載的是:pdftohtml-0.39-win32.tar.gz
4、解壓調(diào)試
1) 先將xpdf-3.02pl2-win32.zip解壓,解壓后的內(nèi)容可根據(jù)需要進(jìn)行刪減,如果只需要轉(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)容為:
- #----- 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
#----- 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)容為:
- @echo off
- set folderPath=%1
- set filePath=%2
- cd /d %folderPath%
- pdftohtml -enc GBK %filePath%
- exit
@echo off
set folderPath=%1
set filePath=%2
cd /d %folderPath%
pdftohtml -enc GBK %filePath%
exit
7) 創(chuàng)建類
- 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();
- }
- }
- }
-
- }
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文件進(jìn)行轉(zhuǎn)換
8) 測試轉(zhuǎn)換
- public static void main(String[] args)
- {
- ConvertPdf.convertToHtml("C:\\test.pdf", "X:\\xpdf");
- }
public static void main(String[] args)
{
ConvertPdf.convertToHtml("C:\\test.pdf", "X:\\xpdf");
}
以上就是整理的內(nèi)容,后續(xù)還會添加視頻轉(zhuǎn)換為FLV格式,如果需要的話:)
說的不是很詳細(xì),碰到問題的朋友可以自己努力解決一下:)