前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留
因此决定换一种处理方式
Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供自动化访问MS系统下COM组件和Win32 libraries的功能。
1.准备
(1)安装MS Office
(2)使用spring boot 框架
(3)pom.xml 添加 jacob 依赖
<dependency> <groupId>net.sf.jacob-project</groupId> <artifactId>jacob</artifactId> <version>1.14.3</version> </dependency>
(4)安装dll
在https://mvnrepository.com/ 查询jacob,选择第一个
选择适合自己机器的dll文件
将下载下来的dll文件放在
C:\Program Files\Java\jdk1.8.0_151\bin C:\Program Files\Java\jdk1.8.0_151\jre\bin C:\WINDOWS\system32 C:\Program Files\Java\jre1.8.0_151\bin
其实只要在 C:\WINDOWS\system32下就可以找到了
2.使用
- MS系统提供的COM组件
COM组件 | 对象ID |
---|---|
MS Word | Word.Application |
MS Excel | Excel.Application |
MS Powerpoint | Powerpoint.Application |
重要的类和方法
JacobObject:用于Java程序MS下的COM进行通信,创建标准的API框架 ComThread:初始化COM组件线程,释放线程,对线程进行管理 Dispatch:调度处理类,封装了操作来从而操作Office,并表示不同MS级别调度对象 Dispatch.get(dispatch, String name);获取对象属性 Dispatch.put(dispatch, String name, Object value);设置对象属性 Dispatch.call(dispatch, String name, Object… args);调用对象方法 ActiveXComponent : 创建COM组件 Variant : 与COM通讯的参数或者返回值
可以参考VBA API,使用Jacob操作COM组件 https://docs.microsoft.com/en-us/office/vba/api/powerpoint.slides.insertfromfile 查看
处理过程
初始化ComThread——》初始化应用——》设置应用属性——》获取应用属性或对象,设置属性参数——》调用属性对应的方法
(1)ppt转pdf
public BackRS ppt2Pdf(String sourcePath, String destPath,BackRS rs){ ActiveXComponent ppt = null; Dispatch presentations = null; try { ComThread.InitMTA(true); ppt = new ActiveXComponent("PowerPoint.application"); Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false)); presentations = ppt.invokeGetComponent("Presentations") .invokeGetComponent("Open", new Variant(sourcePath)); Dispatch.invoke(presentations, "SaveAs", Dispatch.Method, new Object[]{destPath, new Variant(32)}, new int[1]); rs.setFlag(true); rs.setMsg("pdf successfully converted."); rs.setPath(destPath); rs.setFileName(sourcePath); } catch (Exception e) { rs.setFlag(false); rs.setMsg(e.getMessage()); } finally { try { if (ppt != null) { ppt.invoke("Quit"); } } catch (Exception e) { rs.setFlag(false); rs.setMsg(e.getMessage()); } finally { if (presentations != null) presentations.safeRelease(); if (ppt != null) ppt.safeRelease(); ComThread.Release(); } } return rs; }
(2)ppt按页保存图片
public static final int PPT_SAVEAS_JPG = 17; public BackRS converter(String fileName){ BackRS rs = new BackRS(); ActiveXComponent ppt = null; Dispatch presentations = null; String source = fileName; File file = new File(source); if (!file.exists()) { rs.setFlag(false); rs.setMsg("转换文件不存在"); return rs; } String filePath = file.getParent()+File.separator; String dest = filePath + getFileNameNoEx(file.getName())+"_JPG"; File destPath = new File(dest); if (!destPath.exists()) { destPath.mkdir(); } try { ComThread.InitMTA(true); ppt = new ActiveXComponent("PowerPoint.application"); Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false)); presentations = ppt.invokeGetComponent("Presentations").invokeGetComponent("Open", new Variant(source)); saveAs(presentations, dest, PPT_SAVEAS_JPG); rs.setFlag(true); rs.setMsg("Image successfully converted."); rs.setPath(dest); rs.setFileName(fileName); } catch (Exception e) { rs.setFlag(false); rs.setMsg(e.getMessage()); } finally { try { if (ppt != null) { ppt.invoke("Quit"); } } catch (Exception e) { rs.setFlag(false); rs.setMsg(e.getMessage()); } finally { if (presentations != null) presentations.safeRelease(); if (ppt != null) ppt.safeRelease(); ComThread.Release(); } } return rs; } public void saveAs(Dispatch presentation, String saveTo, int ppSaveAsFileType)throws Exception { Dispatch.call(presentation, "SaveAs", saveTo, new Variant( ppSaveAsFileType)); }
(3)ppt合并
public void merge(String outPutPPTPath, List<String> mergePPTPathList) { // 启动 office PowerPoint程序 ActiveXComponent pptApp = null; Dispatch presentations = null; Dispatch outputPresentation; try { ComThread.InitMTA(true); pptApp = new ActiveXComponent("PowerPoint.Application"); Dispatch.put(pptApp, "Visible", new Variant(true)); presentations = pptApp.getProperty("Presentations").toDispatch(); File file = new File(outPutPPTPath); if (file.exists()) { file.delete(); } file.createNewFile(); // 打开输出文件 outputPresentation = Dispatch.call(presentations, "Open", outPutPPTPath, false, false, true).toDispatch(); // 循环添加合并文件 for (String mergeFile : mergePPTPathList) { Dispatch mergePresentation = Dispatch.call(presentations, "Open", mergeFile, false, false, true).toDispatch(); Dispatch mergeSildes = Dispatch.get(mergePresentation, "Slides").toDispatch(); int mergePageNum = Integer.parseInt(Dispatch.get(mergeSildes, "Count").toString()); // 关闭合并文件 Dispatch.call(mergePresentation, "Close"); Dispatch outputSlides = Dispatch.call(outputPresentation, "Slides").toDispatch(); int outputPageNum = Integer.parseInt(Dispatch.get(outputSlides, "Count").toString()); // 追加待合并文件内容到输出文件末尾 Dispatch.call(outputSlides, "InsertFromFile", mergeFile, outputPageNum, 1, mergePageNum); } // 保存输出文件,关闭退出PowerPonit. Dispatch.call(outputPresentation, "Save"); Dispatch.call(outputPresentation, "Close"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pptApp != null) { pptApp.invoke("Quit"); } } catch (Exception e) { e.printStackTrace(); } finally { if (presentations != null) presentations.safeRelease(); if (pptApp != null) pptApp.safeRelease(); ComThread.Release(); } } }
将mergePPTPathList 中的文件,追加到 outPutPPTPath文件中
下面的语句
Dispatch.call(outputSlides, "InsertFromFile", mergeFile, outputPageNum, 1, mergePageNum);
InsertFromFile( _FileName_, _Index_, _SlideStart_, _SlideEnd_ )
参考这个原型,可以实现追加指定的第umPage页面
Dispatch.call(outputSlides, "InsertFromFile",mergeFile, outputPageNum, umPage, umPage);
说明:
(1)出现错误 Can't get object clsid from progid
检查 dll文件都在指定位置,但还是报错
后来发现原来参考的其他人的文章Jacob调用WPS将Office文件转为PDF文件,对象ID不正确
其中 ppt = new ActiveXComponent("KWPP.Application");改为 ppt = new ActiveXComponent("PowerPoint.application");
(2)Can’t load IA 32-bit .dll on a AMD 64-bit platform
出现这个报错是因为使用的jacob.dll和系统不匹配,把32位的用在了64位的系统上了,几位的系统就用几位jacob.dll
(3)如果不想用Office想用WPS,不能安装极小安装包