jacob操作word

cellcomcn 贡献于2012-03-08

作者 liufan  创建于2009-09-25 03:19:00   修改者liufan  修改于2009-10-28 09:00:00字数6901

文档摘要:最近由于工作的原因需要对Word进行操作,并且在文档上面加上水印,这个问题我在网上搜索了很久都没有一个完整的解决办法,没得办法之好自己研究了,半天的努力没有白费,终于解决了,有兴趣的朋友可以参考下。
关键词:

最近由于工作的原因需要对Word进行操作,并且在文档上面加上水印,这个问题我在网上搜索了很久都没有一个完整的解决办法,没得办法之好自己研究了,半天的努力没有白费,终于解决了,有兴趣的朋友可以参考下: 环境: jdk1.4 jacob 1.9 office2003 至于jacob的设置我就不用多说了,网上很多这样的文章,下面就把我的代码贴出来,供大家参考。 package src; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import com.jacob.com.ComThread; public class WordObj {    public WordObj()    {    }    private static WordObj instance;    private Dispatch doc = null;    private Dispatch activeWindow = null;    private Dispatch docSelection = null;    private Dispatch wrdDocs = null;    private String fileName;    private ActiveXComponent wrdCom;    /**     * 获取Word操作静态实例对象     *     * @return 报表汇总业务操作     */    public final static synchronized WordObj getInstance()    {       if (instance == null)          instance = new WordObj();       return instance;    }    /**     * 初始化Word对象     *     * @return 是否初始化成功     */    public boolean initWordObj()    {       boolean retFlag = false;       ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法       wrdCom = new ActiveXComponent("Word.Application");       try       {          // 返回wrdCom.Documents的Dispatch          wrdDocs = wrdCom.getProperty("Documents").toDispatch();          wrdCom.setProperty("Visible", new Variant(true));          retFlag = true;       }       catch (Exception e)       {          retFlag = false;          e.printStackTrace();       }       return retFlag;    }    /**     * 创建一个新的word文档     *     */    public void createNewDocument()    {       doc = Dispatch.call(wrdDocs, "Add").toDispatch();       docSelection = Dispatch.get(wrdCom, "Selection").toDispatch();    }    /**     * 取得活动窗体对象     *     */    public void getActiveWindow()    {       // 取得活动窗体对象       activeWindow = wrdCom.getProperty("ActiveWindow").toDispatch();    }    /**     * 打开一个已存在的文档     *     * @param docPath     */    public void openDocument(String docPath)    {       if (this.doc != null)       {          this.closeDocument();       }       doc = Dispatch.call(wrdDocs, "Open", docPath).toDispatch();       docSelection = Dispatch.get(wrdCom, "Selection").toDispatch();    }    /**     * 关闭当前word文档     *     */    public void closeDocument()    {       if (doc != null)       {          Dispatch.call(doc, "Save");          Dispatch.call(doc, "Close", new Variant(0));          doc = null;       }    }    /**     * 文档设置水印     *     * @param waterMarkStr 水印字符串     */    public void setWaterMark(String waterMarkStr)    {       // 取得活动窗格对象       Dispatch activePan = Dispatch.get(activeWindow, "ActivePane")             .toDispatch();       // 取得视窗对象       Dispatch view = Dispatch.get(activePan, "View").toDispatch();       //输入页眉内容       Dispatch.put(view, "SeekView", new Variant(9));       Dispatch headfooter = Dispatch.get(docSelection, "HeaderFooter")             .toDispatch();       //取得图形对象       Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();       //给文档全部加上水印       Dispatch selection = Dispatch.call(shapes, "AddTextEffect",             new Variant(9), waterMarkStr, "宋体", new Variant(1),             new Variant(false), new Variant(false), new Variant(0),             new Variant(0)).toDispatch();       Dispatch.call(selection, "Select");       //设置水印参数       Dispatch shapeRange = Dispatch.get(docSelection, "ShapeRange")             .toDispatch();       Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject1");       Dispatch textEffect = Dispatch.get(shapeRange,"TextEffect").toDispatch();       Dispatch.put(textEffect, "NormalizedHeight", new Boolean(false));       Dispatch line = Dispatch.get(shapeRange, "Line").toDispatch();       Dispatch.put(line, "Visible", new Boolean(false));       Dispatch fill = Dispatch.get(shapeRange, "Fill").toDispatch();       Dispatch.put(fill, "Visible", new Boolean(true));       //设置水印透明度       Dispatch.put(fill, "Transparency", new Variant(0.5));       Dispatch foreColor = Dispatch.get(fill,"ForeColor").toDispatch(); //设置水印颜色       Dispatch.put(foreColor, "RGB", new Variant(16711680));       Dispatch.call(fill, "Solid");       //设置水印旋转       Dispatch.put(shapeRange, "Rotation", new Variant(315));       Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));       Dispatch.put(shapeRange, "Height", new Variant(117.0709));       Dispatch.put(shapeRange, "Width", new Variant(468.2835));       Dispatch.put(shapeRange, "Left", new Variant(-999995));       Dispatch.put(shapeRange, "Top", new Variant(-999995));       Dispatch wrapFormat = Dispatch.get(shapeRange, "WrapFormat").toDispatch();       //是否允许交叠       Dispatch.put(wrapFormat, "AllowOverlap", new Variant(true));       Dispatch.put(wrapFormat, "Side", new Variant(3));       Dispatch.put(wrapFormat, "Type", new Variant(3));       Dispatch.put(shapeRange, "RelativeHorizontalPosition", new Variant(0));       Dispatch.put(shapeRange, "RelativeVerticalPosition", new Variant(0));       Dispatch.put(view, "SeekView", new Variant(0));    }      /**     * 关闭Word资源     *     *     */    public void closeWordObj()    {       // 关闭word文件       wrdCom.invoke("Quit", new Variant[] {});       // 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理       ComThread.Release();    }    /**     * 得到文件名     *     * @return .     */    public String getFileName()    {       return fileName;    }    /**     * 设置文件名     *     * @param fileName .     */    public void setFileName(String fileName)    {       this.fileName = fileName;    }    /**     * 测试功能     *     */    public static void main(String[] argv)    {       WordObj d = WordObj.getInstance();       try       {          if (d.initWordObj())          {             d.createNewDocument();             d.getActiveWindow();             d.setWaterMark("HEllo");             // d.closeWordObj();          }          else             System.out.println("初始化Word读写对象失败!");       }       catch (Exception e)       {          d.closeWordObj();       }    } } 在linux环境下,java怎么实现从word格式转换为PDF格式方法 import   com.jacob.activeX.ActiveXComponent; import   com.jacob.com.Dispatch; import   com.jacob.com.Variant; /**   *   @author   XuMing   Li   *     *   @version   1.00,   2007-4-9   *       */ public   class   D2P   {         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                         Dispatch   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文件                 wordCom.invoke( "Quit ",   new   Variant[]   {});         }         /**           *   *   将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 "));                         //设置printout的参数,将word文档打印为postscript文档。目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api                         //是否在后台运行                         Variant   Background   =   False;                         //是否追加打印                         Variant   Append   =   False;                         //打印所有文档                         int   wdPrintAllDocument   =   0;                         Variant   Range   =   new   Variant(wdPrintAllDocument);                         //输出的postscript文件的路径                         Variant   OutputFileName   =   new   Variant(destinPSFilePath);                         Dispatch.callN((Dispatch)   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)   {                 D2P   d2p   =   new   D2P();                 //                                 d2p.openWord( "c:/12.doc ");                 //                 d2p.callWordMacro( "c:/12.docc ",   "MyWordMacro ",                 //                                 new   String[]   {   "这是调用word宏的测试程序 "   });                 d2p.docToPDF( "d:/12.doc ",   "c:/1p.ps ",   "c:/1p.pdf ");         } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档