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