1.word用jacob转换成pdf时报错Invoke of: SaveAs,word是2003,在线等
首先word03对这个支持不好.推荐07以上.07sp2之后word才可以直接另存为pdf.以下代码对应的是word2010Java代码 1. public class JacobTest2. public static void wordToPDF(String3. ActiveXComponentword 4. try { 5. app.setProperty(6. Dispatch docs7. Dispatch doc8. docs, 9. "Open",10. Dispatch.Method,11. new Object[]12. new13. //new Variant(type)14. Dispatch.invoke(doc,15. toFile, new16. Variant f = new17. Dispatch.call(doc,18. } catch (Exception19. e.printStackTrace();20. } finally { 21. app.invoke("Quit"22. } 23. } 24. 25. public static void main(String[]26. //源文件全路径27. String docfile ="D:28. for (int i = 0; i <29. 些路径test为实际存在的目录30.="" string="" tofile="31." wordtopdf(docfile,32.="" }="" 33.="" }="" 34.="" }="" jacobtest="" {="" wordtopdf(string="" docfile,="" string="" tofile,int="" type)="" {="" activexcomponent="" app="new" activexcomponent("word.application"app.setproperty("visible",="" new="" variant(false));="" docs="app.getProperty("Documents").toDispatch();" =="" dispatch.invoke(="" dispatch.method,="" object[]="" {="" docfile,="" new="" variant(false),="" new="" variant(true)="" },="" new="" int[1]).todispatch();="" variant(type),这里面的type的决定另存为什么类型的文件dispatch.invoke(doc,="" "saveas",="" dispatch.method,="" new="" object[]="" new="" variant(type)="" },="" new="" int[1]);="" new="" variant(false);="" dispatch.call(doc,="" "close",="" f);="" (exception="" e)="" {="" e.printstacktrace();="" "quit",="" new="" variant[]="" {});="" main(string[]="" args)="" {="" 源文件全路径="" "d:\\服务实施描述报告(企业门户).docx";="">29.>< 18;="" i++)="" {="" 为实际存在的目录,s后面为要另存为的文件名tofile="d:\\test\\s" +i;="" wordtopdf(docfile,="" tofile,i);="" "word.application");="" 启动="">
2.如何用纯java代码实现word转pdf
几种方案:方法一:用apachepio读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便地址:安装完后要启动openOffice的服务,具体启动方法请自行google方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件SaveAsPDFandXPS下载地址:/zh-cn/download/details.aspx?id=7jacob包下载地址:。
3.java中的jacob将word文档转化为HTML文件问题
1、到官网下载Jacob,2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);3、将Jacob.dll放至当前项目所用到的“jre\bin”下面(比如Eclipse正在用的Jre路径是C:\Java\jdk1.7.0_17\jre\bin)。
Ps:按照上面的步骤配置的,基本没有问题,但是有些电脑可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWS\SYSTEM32”下面。Java代码:public class JacobUtil { // 8 代表word保存成html public static final int WORD_HTML = 8; public static void main(String[] args) { String docfile = "C:\\Users\\无名\\Desktop\\xxx.doc"; String htmlfile = "C:\\Users\\无名\\Desktop\\xxx.html"; JacobUtil.wordToHtml(docfile, htmlfile); } /** * WORD转HTML * @param docfile WORD文件全路径 * @param htmlfile 转换后HTML存放路径 */ public static void wordToHtml(String docfile, String htmlfile) { // 启动word应用程序(Microsoft Office Word 2003) ActiveXComponent app = new ActiveXComponent("Word.Application"); System.out.println("*****正在转换。
*****"); try { // 设置word应用程序不可见 app.setProperty("Visible", new Variant(false)); // documents表示word程序的所有文档窗口,(word是多文档应用程序) Dispatch docs = app.getProperty("Documents").toDispatch(); // 打开要转换的word文件 Dispatch doc = Dispatch.invoke( docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); // 作为html格式保存到临时文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(WORD_HTML) }, new int[1]); // 关闭word文件 Dispatch.call(doc, "Close", new Variant(false)); }catch (Exception e){ e.printStackTrace(); }finally{ //关闭word应用程序 app.invoke("Quit", new Variant[] {}); } System.out.println("*****转换完毕********"); } }。
转载请注明出处51数据库 » jacobword转pdf
亖呉?盀