jacob操作文档转换底层还是调用windows的 office去转换,office是建立在windows平台之上的,本身是一个软件,除了他自己提供的宏似乎没有什么能对他进行直接的操作,在windows平台上为了解决像这样的不同应用软件,通信缺乏通用api问题,推出了com的解决方案。我们使用dll中的一组或多组相关的函数存取组件数据,总的合称为接口(对应jacob,就是Dispatch),具体到每个细节的实现称为方法。我们使用jacob就是通过一个接口来操作word的ActiveX对象(实质是调用指向接口的指针,这也是唯一途径)。
所以在使用jacob进行文档转换时需要用到jacob-1.19-x64.dll;jacob-1.19-x86.dll去做底层的操作。
//word转pdf
public static void doc2PDF(String srcFilePath, String pdfFilePath) throws Exception {
ActiveXComponent app = null;
Dispatch doc = null;
try {
//初始化COM组件线程
ComThread.InitSTA();
//创建com组件,打开word应用程序
app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", new Variant(false));
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
Object[] obj = new Object[] { srcFilePath, new Variant(false), new Variant(false), // 是否只读
new Variant(false), new Variant("pwd") };
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();//
//Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
Dispatch.put(doc, "RemovePersonalInformation", false);
// word保存为pdf格式宏,值为17
Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
//结束后关闭线程组件
ComThread.Release();
}
}
//ppt转pdf
public static void ppt2PDF(String srcFilePath, String pdfFilePath) throws Exception {
ActiveXComponent app = null;
Dispatch ppt = null;
try {
ComThread.InitSTA();
app = new ActiveXComponent("PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch();
/*
* call param 4: ReadOnly param 5: Untitled指定文件是否有标题 param 6: WithWindow指定文件是否可见
*/
ppt = Dispatch.call(ppts, "Open", srcFilePath, true, true, false).toDispatch();
Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值32
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (ppt != null) {
Dispatch.call(ppt, "Close");
}
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
ComThread.Release();
}
}
//excel转pdf
public static void excel2PDF(String inFilePath, String outFilePath) throws Exception {
ActiveXComponent ax = null;
Dispatch excel = null;
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Excel.Application");
ax.setProperty("Visible", new Variant(false));
ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = ax.getProperty("Workbooks").toDispatch();
Object[] obj = new Object[] { inFilePath, new Variant(false), new Variant(false) };
excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
// 转换格式
Object[] obj2 = new Object[] { new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
outFilePath, new Variant(0) // 0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件
};
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (excel != null) {
Dispatch.call(excel, "Close", new Variant(false));
}
if (ax != null) {
ax.invoke("Quit", new Variant[] {});
ax = null;
}
ComThread.Release();
}
}
注:路径问题
inFilePath:当前文档所在位置;outFilePath:转pdf后的文件保存位置
如部署在tomcat的某个文件下:
ServletContext servletContext = getRequest().getSession().getServletContext();
String realPath = servletContext.getRealPath("/");
String inFilePath = realPath + "/data/supp/file_upload/PRJFILE/"+recordId;
String outFilePath = realPath +"/data/supp/file_upload/PRJFILE/pdf/"+recordId+".pdf";