有需求要进行页面打印,要打印word文档中的内容,看到jacob可以把word文档转换为html,正好可以在客户端打印该html页面,满足页面打印的需要!
把代码粘出来与大家分享!
- ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
- try {
- app.setProperty("Visible", new Variant(false));
- //设置word不可见
- 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();
- // 打开word文件 docfile为word文件的路径
- Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile,
- new Variant(8) }, new int[1]);
- // 作为html格式保存到临时文件 htmlfile为要保存的html文件的路径
- Dispatch.call(doc, "Close", new Variant(false));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- app.invoke("Quit", new Variant[]{});//退出
- }