c语言Jacob例子,Jacob操作ppt
前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留
因此决定换一种处理方式
Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供自动化访问MS系统下COM组件和Win32 libraries的功能。
1.准备
(1)安装MS Office
(2)使用spring boot 框架
(3)pom.xml 添加 jacob 依赖
net.sf.jacob-project
jacob
1.14.3
(4)安装dll
在https://mvnrepository.com/ 查询jacob,选择第一个
选择适合自己机器的dll文件
将下载下来的dll文件放在
C:Program FilesJavajdk1.8.0_151in
C:Program FilesJavajdk1.8.0_151jrein
C:WINDOWSsystem32
C:Program FilesJavajre1.8.0_151in
其实只要在 C:WINDOWSsystem32下就可以找到了
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, Stringname);获取对象属性
Dispatch.put(dispatch, String name, Objectvalue);设置对象属性
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
publicBackRS 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", newVariant(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();
}
}returnrs;
}
(2)ppt按页保存图片
public static final int PPT_SAVEAS_JPG = 17;publicBackRS converter(String fileName){
BackRS rs= newBackRS();
ActiveXComponent ppt= null;
Dispatch presentations= null;
String source=fileName;
File file= newFile(source);if (!file.exists()) {
rs.setFlag(false);
rs.setMsg("转换文件不存在");returnrs;
}
String filePath= file.getParent()+File.separator;
String dest= filePath + getFileNameNoEx(file.getName())+"_JPG";
File destPath= newFile(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", newVariant(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();
}
}returnrs;
}public voidsaveAs(Dispatch presentation, String saveTo,int ppSaveAsFileType)throwsException {
Dispatch.call(presentation,"SaveAs", saveTo, newVariant(
ppSaveAsFileType));
}
(3)ppt合并
public void merge(String outPutPPTPath, ListmergePPTPathList) {//启动 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= newFile(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,不能安装极小安装包
c语言Jacob例子,Jacob操作ppt相关推荐
- c语言 文件课件,C语言课件第13章-文件操作.ppt
C语言课件第13章-文件操作.ppt 第13章文件操作 哈尔滨工业大学计算机科学与技术学院苏小红sxh 本章学习内容 二进制文件和文本文件 文件的打开和关闭 文件的顺序读写与随机读写 标准输入输出及其 ...
- pythonppt_python操作ppt下载
这是python操作ppt,包括了python概述,python基础语法,Python数据类型,条件和循环,函数,模块,面向对象编程,文件相关等内容,欢迎点击下载. python操作ppt是由红软PP ...
- c语言经典解决实际程序,C语言经典教程1讲.ppt
<C语言经典教程1讲.ppt>由会员分享,可在线阅读,更多相关<C语言经典教程1讲.ppt(48页珍藏版)>请在人人文库网上搜索. 1.C程序设计,主讲人:任祖华,2,本课程学 ...
- c语言中二维数组中维数的计算,数组指针字符串C语言程序设计-第4章.ppt
<数组指针字符串C语言程序设计-第4章.ppt>由会员分享,可在线阅读,更多相关<数组指针字符串C语言程序设计-第4章.ppt(132页珍藏版)>请在人人文库网上搜索. 1.程 ...
- 北京科技大学C语言程序设计,北京科技大学《C语言》第1章.ppt
<北京科技大学<C语言>第1章.ppt>由会员分享,可在线阅读,更多相关<北京科技大学<C语言>第1章.ppt(36页珍藏版)>请在装配图网上搜索. 1 ...
- c语言第1章ppt,c语言第1章课件.ppt
<c语言第1章课件.ppt>由会员分享,可在线阅读,更多相关<c语言第1章课件.ppt(30页珍藏版)>请在人人文库网上搜索. 1.第1章 C语言概述,计算机中心,C 语言程序 ...
- c语言程序设计课件第二章,c语言程序设计课件张元国 ISBN9787566300386 PPT第二章数据类型 运算符与表达式...
1.第2章 数据类型.运算符与表达式,语言的数据类型 常量与变量 运算符与表达式 不同类型数据间的转换,2.1语言的数据类型,数据是计算机程序处理的所有信息的总称,数值.字符.文本等都是数据,在各种程 ...
- 0927_C/C++笔试题_10:16道c语言面试例子【2】
16道c语言面试例子[2]: 2.写一个"标准"宏MIN,这个宏输入两个参数并返回较小的一个. #define MIN(x,y) ((x)<=(y)?(x):(y)) 这个测 ...
- c语言课程设计作业五子棋,c语言课程设计案例-五子棋.ppt
<c语言课程设计案例-五子棋.ppt>由会员分享,可在线阅读,更多相关<c语言课程设计案例-五子棋.ppt(25页珍藏版)>请在人人文库网上搜索. 1.C语言综合编程训练,C程 ...
最新文章
- 程序员感叹一年只能存下15万太少了……网友:潸然泪下
- 交换机的linux测试脚本,更新网络设备巡检脚本,各位大神可以看看,如有不足,请指正!...
- 通过程序获得SQL Server自增型字段的函数:GetKey
- Android向本地写入一个XML文件和解析XML文件
- CG CTF WEB 单身二十年
- 英语音标 语言、语音、音素及音标
- Linux centos7 安装 MySQL5.7.x
- SQLAlchemy ORM教程之三:Relationship
- jq选中单选框后文本框不可编辑_3个Excel工作表的数据输入编辑技巧,助你工作更轻松...
- 腾讯内容开放平台:将重拳打击盗号 严重违法行为将报送国家执法部门
- 使用MyBatis框架连接MySQL数据库查询记录,全部步骤
- PHP实现菱形与杨辉三角形【php趣味案例】
- 不定期总结程序员常见误区
- 51单片机跑马灯c语言,51单片机——跑马灯详解(示例代码)
- I2S协议及在verilog中实现
- 【网络基础】通俗易懂的搞明白什么是IP地址(小白向)
- 谷歌是如何跌下神坛的?
- mac系统下修改usr/bin文件夹权限问题
- 脚本恢复Win10照片查看器
- 深度学习经典试题29道