如何使用jacob将word转换为PDF

来源:互联网 发布:针织毛衣品牌推荐知乎 编辑:程序博客网 时间:2024/04/29 23:18

一、代码实现功能介绍:

word文档转换为PDF文件的功能。

二、使用的环境条件:

正是由于jacob的jni这种特性,所以运行jacob的机器上必须要有jacob.dll在系统的path中,而且还要有相应的被调用的com组件存在。

下面列表说明了本项目使用的软件环境和一些注意事项:
  1.Word2003、Adobe Acrobat 8.0 Professional(注:也可以使用7.5以上版本,我在做的时候,7.0版本有一些BUG,后又重新安装了8.0版本,安装地址及破解地址分别为:
http://down1.greendown.cn//200611/AcroPro80_efg.rar
http://soft.greendown.cn//200611/AcroPro80_Crack.rar);
  2.并且关闭了Adobe PDF打印机属性->Adobe PDF Setting中的“do not send fonts to PDF”选项(注:此属性在Adobe Reader中的文件->打印的属性中进行设置。如果使用7.5以下版本可能会不好用,无法进行设置);
  3.安装了gs811w32.rar(PDF转换时所需要的脚本ps),地址为:
http://www.allmail.com.cn/gs811w32.rar
  4.安装了postscript.rar(PDF虚拟打印机的驱动),地址为:
http://www.pdfhome.com.cn/Resource/DownLoad/postscript.rar
虚拟打印机安装完成之后,控制面板》打印机及其他硬件》打印机和传真》添加打印机(如果添加时显示“操作无法完成。打印后台程序服务没有运行。”请打开控制面板》性能和维护》管理工具》服务》找到“Print Spooler”》右击属性》启动)》选择本地打印机(如果没有打印机请将“检测并安装即插打印机”的钩去掉)》下一步》选择“使用以下端口”(My Document/*.pdf  (Adobe PDF Port))》下一步,记住打印机的名字:Adobe PDF)》下一步(打印机请选择:不测试)
  5.下载得到了jacob.jar。地址为:
http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368
将解压后的源代码包中jacob.dll存放到system32目录下。(注:目前比较稳定的版本分别为1.7、1.8、1.9,我用的是1.8版本)

注意:

第1、2个条件是必须的。不然会可能出现下面的错误:

三、原理:

doc -> ps ->pdf  >>>>    office 2003 ->gs811w32->Adobe Acrobat 8->postscript->打印机

四、源代码:

package com.bjinfotech.practice.jacob;

import com.jacob.com.*;
import com.jacob.activeX.*;

public class Dispatch_MSWord {
      private ActiveXComponent wordCom=null;
      private Object wordDoc=null;
      private final Variant False=new Variant(false);
      private final Variant True=new Variant(true);
       
        /**
         * 打开word文档
         * @param filePath word文档
         * @return 返回word文档对象
         */
      public boolean openWord(String filePath){
                //建立ActiveX部件
                wordCom=new ActiveXComponent("Word.Application");               
                try{
                        //返回wrdCom.Documents的Dispatch
                        Object wrdDocs=wordCom.getProperty("Documents").toDispatch();
                        //调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
                        wordDoc=Dispatch.invoke(wrdDocs,"Open",Dispatch.Method,new Object[]{filePath},new int[1]).toDispatch();
                        return true;
                }
                catch(Exception ex){
                        ex.printStackTrace();
                }
                return false;
        }
       
        /**
         * 关闭word文档
         */
     public void closeWord(){
                //关闭word文件
             if (wordCom!=null){
           int save = 0;
           Variant doNotSaveChanges = new Variant(save);
                       wordCom.invoke("Quit",new Variant[]{doNotSaveChanges});
             wordCom=null;                    
           ComThread.Release();
      }
     }       
       
        /**
         * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件
         * @param sourceFilePath 源文件路径
         * @param destinPSFilePath 首先生成的PS文件路径
         * @param destinPDFFilePath 生成PDF文件路径
         */
     public void docToPDF(String sourceFilePath,String destinPSFilePath,String destinPDFFilePath){
                if (!openWord(sourceFilePath)){
                        closeWord();
                        return;
                }
                //建立Adobe Distiller的com对象
                ActiveXComponent distiller=new ActiveXComponent("PDFDistiller.PDFDistiller.1");
                try{
                //设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF"
                wordCom.setProperty("ActivePrinter",new Variant("Adobe PDF"));                      

                //是否在后台运行
                Variant Background=False;
                //是否追加打印
                Variant Append =False;
                //打印所有文档
                int wdPrintAllDocument=0;
                Variant Range =new Variant(wdPrintAllDocument);
                //输出的postscript文件的路径
                Variant OutputFileName =new Variant(destinPSFilePath);            
                //调用word文档对象的PrintOut方法:将word文档打印为postscript文档,简称ps文档
                Dispatch.callN(wordDoc, "PrintOut", new Variant[]{Background,Append,Range,OutputFileName}) ;
                System.out.println("由word文档打印为ps文档成功!");
               
                //调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册
                //作为输入的ps文档路径
                Variant inputPostScriptFilePath=new Variant(destinPSFilePath);
                //作为输出的pdf文档的路径
                Variant outputPDFFilePath=new Variant(destinPDFFilePath);
                //定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件
                Variant PDFOption=new Variant("");
                //调用FileToPDF方法将ps文档转换为pdf文档
                Dispatch.callN(distiller,"FileToPDF",new Variant[]{inputPostScriptFilePath,outputPDFFilePath,PDFOption});
                System.out.println("由ps文档转换为pdf文档成功!");               
                }
                catch(Exception ex){
                        ex.printStackTrace();
                }
                finally{
                        closeWord();
                }
        }
       
        public static void main(String[] argv){
                Dispatch_MSWord d=new Dispatch_MSWord();
                d.docToPDF("c:/MacroTest.doc","c:/1p.ps","c:/1p.pdf");
        }
}

 
五、参考资源:

http://www.matrix.org.cn/resource/article/2005-11-04/jacob_word_PDF_43923.html
http://blog.csdn.net/hcx_2008/archive/2007/09/10/1779534.aspx