在linux环境下,java怎么实现从word格式转换为PDF格式方法。

解决方案 »

  1.   


        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 "); 
            } 

      

  2.   

    你真是太幸运了,我刚发到博客将office文档(word,excel,powerpoint)转换为pdf
    http://www.codigg.com/2009/05/office-word-excel-ppt-to-pdf-java/
    本文中的方法可以将Office文档(word,excel,powerpoint)转为pdf文档。代码中使用了jcom(http://sourceforge.net/projects/jcom),日本的一个项目。
      

  3.   

    2楼  刚从你的博客回来!这个类怎么用啊!能给菜鸟级别的人讲解一下不!我都不知道怎么导入包!
        import   com.jacob.activeX.ActiveXComponent; 
    import   com.jacob.com.Dispatch; 
    import   com.jacob.com.Variant; 
      

  4.   

    有这么复杂?
    openoffice3直接输出不好?
      

  5.   

    Windows XP下建议使用Adobe Acrobat 6 Professional或是更高版本!
      

  6.   

    有pdf的 文本转化器 也有制作器
    不过挺麻烦!
      

  7.   

    可以使用OpenOffice,很容易手动转换。
    也可以调用OpenOffice的API。
      

  8.   

    仍然依赖于adobe acrobat pro版.
      

  9.   

    it seems the jacob libiary did not support ubuntu circumstance, can't work out here.
      

  10.   

    那word里面的图片和表格怎么转到pdf里面?
      

  11.   

    word和PDF不符合我国文档格式标准,严重影响中国国家信息安全,要坚决抵制。
      

  12.   

    office2007里就有,何必那么烦呀
      

  13.   


    ---------------------------
    这个代码应该只能在window上运行,使用了window上的ActiveX控件
      

  14.   


    顶,比如这个http://www.artofsolving.com/opensource/jodconverter
      

  15.   

    用 PDFCreator----------------------------------------------------------
    Java高手群:28840096(Java乐园2群), 欢迎Javaer加入。
    Java乐园站:http://www.javaly.cn
      

  16.   

    2,3楼方法在linux下应该是不行的,都是通过java去调用com接口,只能在windows下用。
    楼之可以看看Apache的POI项目, 读取word文件然后用itext生成pdf。
      

  17.   

    JDOM是一个不错的API,不过,日本的工程师已经不再打算开发了。也可以尝试调用JCOM