文档转换功能


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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值