Java 使用jacob实现doc转pdf(附带其他方法分析)

 关于doc转pdf的方法根据本人的查询主要有四种:

1.使用Jacob,但是使用jacob中要依赖Office,部分博文中还会依赖插件,如SaveAsPDFandXPS.exe。但是也发现不需要依赖Office,可以使用wps、pdfcreator,在使用wps的时候还不需要安装插件(注意:wps有linux版,office到现在为止还没有linux版)。

2.OpenOffice,可以结合Jodconverter开源框架和OpenOffice.org办公软件,具有跨平台的优点,转化速度快,但是部分office的格式似乎不支持。

3.Adobe Acrobat + jacob,这个用到什么虚拟打印机,和微软的一起使用效果比较好。(这个我不太懂)

4.Jcom + Adobe Acrobat ,会用到IDispatch。

方法3,4好像都依赖office。

我使用的是jacob,需要jacob.jar来调用activex控件,本机需安装WPS,当然使用pdfcreator的话,就要安装它了。 
还需要jacob.jar以及jacob.dll 请看附件 
jacob.dll 需要放置在系统system32下,如果系统是c盘:C://windows/system32/下面 

我是把jacob.dll放在类似这样的目录下,D:\JDK\jdk1.8.0_05\jre\bin。

public class Wps implements Converter {  //接口中只有一个方法,不过添加了同步
    	  
        public synchronized boolean convert(String word, String pdf) {  
        	boolean boo = wps2pdf(word, pdf);
            return boo;  
        }  
        
        public static boolean wps2pdf(String word,String pdf){  
        	File pdfFile = new File(pdf);  
            ActiveXComponent wps = null;  
            Dispatch doc = null;
            long start = System.currentTimeMillis();
            try {  
                wps = new ActiveXComponent("kwps.application");  
                wps.setProperty("visible", new Variant(false));  
                Dispatch docs = wps.getProperty("Documents").toDispatch();
                
//                doc = Dispatch.invoke(docs, "Open", Dispatch.Method, 
//                		new Object[]{word, new Variant(false),new Variant(true)},
//                		new int[1]).toDispatch();
                doc = Dispatch.call(docs, "Open", word, false, true).toDispatch();
                if(pdfFile.exists()){
                	pdfFile.delete();
                }
//                Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{pdf,new Variant(17)}, new int[1]);
                Dispatch.call(doc, "SaveAs", pdf, 17);
                long end = System.currentTimeMillis();
                System.out.println("耗时:" + (end-start) + "ms.");
            } catch (Exception ex) {  
                ex.printStackTrace();
                System.out.println("转化出错:" + ex.getMessage());
                return false;  
            } finally {  
            	Dispatch.call(doc, "Close", false);
            	System.out.println("关闭WPS");
                if (wps != null) {  
                    wps.invoke("Quit", new Variant[]{});
                }  
            }  
            return true;  
        } 
    public static void main(String[] args) {  
        new Wps().convert("E:/tmp/2016/testItext/typ_doc.doc", "E:/tmp/2016/testItext/typ_doc_wpsBycall.pdf");  
    } 
} 

可以参考的资料:

jacob:

用call方法,需装office

http://blog.sina.com.cn/s/blog_4cb1fb310101759c.html

用invoke方法,需装office,要安装插件

http://blog.csdn.net/niuhea/article/details/8611359

调用WPS或pdfcreator的com接口实现doc转pdf(相对比较好)

http://hacker507.iteye.com/blog/1458790

 关于jacob介绍比较详细(包括类,方法)

http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html

jacob中代码及说明:

http://coderbase64.iteye.com/blog/2077184?utm_source=tuicool&utm_medium=referral

jcom:

http://liangjian103.iteye.com/blog/697979

openoffice:

http://huangronaldo.iteye.com/blog/1628339

http://titanseason.iteye.com/blog/1471606

附件中包含jacob-1.18的jar、dll、api_docs

猜你喜欢

转载自itommy.iteye.com/blog/2297235