利用wps将word转pdf

来源:互联网 发布:手机自动接听软件 编辑:程序博客网 时间:2024/04/29 17:36

需安装wps软件:
需要的jar包 jcom.jar  
需要jcom.dll文件  windows32bit放置于system32目录下,windows64bit放置于jdk>bin目录下

package com.mcc.service.impl;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
public class Transformer {
  /**
     * 文档格式转换  
     * @param inPath 文档路径  
     * @param outPath pdf路径  
     */  
    public static void wps2pdf(String inDocPath,String outPdfPath){  
        ReleaseManager rm = new ReleaseManager();  
        try {  
            //支持 wps、wpt、doc、docx、dot、txt、  
            IDispatch wpsApp = new IDispatch(rm, "WPS.Application");  //  
            wpsApp.put("Visible", new Boolean(false)); //是否可见  
            IDispatch wpsDocuments = (IDispatch) wpsApp.get("Documents");  
            IDispatch wpsDocument = (IDispatch) wpsDocuments.method("Open", new String[]{inDocPath});  
            wpsDocument.method("ExportPdf", new String[]{outPdfPath,"","1"});  
            wpsApp.method("Quit", null);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            rm.release();   
        }  
    }  
      
    public static void et2pdf(String inDocPath,String outPdfPath){  
        ReleaseManager rm = new ReleaseManager();  
        try {  
            //et、ett、xls、xlsx、xlt、uof、prn、csv……  
            IDispatch wpsApp = new IDispatch(rm, "ET.Application");  //  
            wpsApp.put("Visible", new Boolean(false)); //是否可见  
            IDispatch wpsDocuments = (IDispatch) wpsApp.get("Workbooks");  
            IDispatch wpsDocument = (IDispatch) wpsDocuments.method("Open", new String[]{inDocPath});  
            wpsDocument.method("ExportPdf", new String[]{outPdfPath,"","1"}); 
            wpsApp.method("Quit", null);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            rm.release();  
        }  
    }  
      
    public static void dps2pdf(String inDocPath,String outPdfPath){  
        ReleaseManager rm = new ReleaseManager();  
        try {  
            //ppt、pps、pptx、ppsx、dps、dpt、pot、uof……  
            IDispatch wpsApp = new IDispatch(rm, "WPP.Application");  //  
            wpsApp.put("Visible", new Boolean(false)); //是否可见  
            IDispatch wpsDocuments = (IDispatch) wpsApp.get("Presentations");  
            IDispatch wpsDocument = (IDispatch) wpsDocuments.method("Open", new String[]{inDocPath});  
            wpsDocument.method("ExportPdf", new String[]{outPdfPath});  
            wpsApp.method("Quit", null);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            rm.release();  
        }  
    }  
 
    public static void main(String[] args) throws Exception {
     wps2pdf("d:\\test.doc", "d:\\one.pdf");
    }  
 
 
 
}

原创粉丝点击