Word 转 PDF

//Word 转 PDF
public class Doc2PDF {
      private ActiveXComponent wordCom=null;
      private Dispatch 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文件
      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){
                Doc2PDF d=new Doc2PDF();
                d.docToPDF("c:\\tmp\\w.doc","c:\\tmp\\ww.ps","c:\\tmp\\www.pdf");
        }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值