public static void convertWordToHtml(String srcPath, String desPath)
throws JacobException {
ActiveXComponent wordApp = new ActiveXComponent("Word.Application");
try {
wordApp.setProperty("Visible", new Variant(false));
//获取word文档类型对象
Dispatch documents = wordApp.getProperty("Documents").toDispatch();
//打开word文档操作
Dispatch doc = Dispatch.invoke(
documents,
"Open",
Dispatch.Method,
new Object[] { srcPath, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
//Add by begin
Object webOption = Dispatch.get(doc, "WebOptions").toDispatch();
//65001 对应的编码为UTF-*
Variant varent = new Variant("65001");
Dispatch.put((Dispatch) webOption, "Encoding", varent);
//Add by end
//将打开的word文档以html形式保存。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
desPath,
new Variant(DocumentConverterConstants.WORD_HTML_SIMPLE) },
new int[1]);
logger.info("Converting Word doTo HTML Successful!");
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
//退出对word相关应用的调用
wordApp.invoke("Quit", new Variant[] {});
}
}
public static void convertWordToPdf(String srcPath, String desPath)
throws JacobException {
ActiveXComponent wordApp = new ActiveXComponent("Word.Application");
try {
wordApp.setProperty("Visible", new Variant(false));
Dispatch documents = wordApp.getProperty("Documents").toDispatch();
//打开word文档操作
Dispatch doc = Dispatch.invoke(
documents,
"Open",
Dispatch.Method,
new Object[] { srcPath, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
//将打开的word文档以pdf形式保存。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
new Object[] { desPath,
new Variant(DocumentConverterConstants.WORD_PDF) },
new int[1]);
logger.info("Converted Word To PDF Successful!");
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
//退出对word相关应用的调用
wordApp.invoke("Quit", new Variant[] {});
}
}
/**
* 转换Office Word文档格式
*
* @param handler
* ,Word文档内容字节流
* @param aJsonStr
* ,Word文档信息JSON格式字符串,包括文件名和转换目标文档格式类型(PDF或HTML)
* @return 成功返回的目标文档内容字节流
* @throws DocumentConverterException
*/
public DataHandler convertOfficeWord(DataHandler handler, String aJsonStr)