java word,excel,ppt转pdf
创始人
2024-02-25 12:04:45
0

准备工作

1.下载 jacob.jar 
链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 
提取码:abcd

2.下载安装wps
WPS Office-支持多人在线编辑多种文档格式_WPS官方网站

3.添加 jar到项目和ddl文件放在jdk的jre/bin目录下,记得自己系统是64位还是32位的

ddl文件放jdk目录下

系统没有设置javahome的记得设置下

path加上

 4.添加jacob.jar到项目中,我用的是idea springboot maven项目,执行maven打包到项目中

mvn install:install-file -Dfile=d:\jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19 -Dpackaging=jar

红色字体换成自己的

com.jacobjacob1.19

5.准备工作完成了我们上代码

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;public class ToPdfTest {private static final int wdFormatPDF = 17;private static final int xlsTypePDF = 0;private static final int ppSaveAsPDF = 32;public static void main(String[] args) {String path = "D:\\222.ppt"; // 自己在对应的位置放上需要转换的文件String pdfPath = "D:\\test222.pdf";// pdfPath为生成PDF文件路径//boolean bo = ToPdfTest.wordtoPDF(path, pdfPath); //word 转//boolean bo = ToPdfTest.exceltoPDF(path, pdfPath); //excel转boolean bo = ToPdfTest.ppttoPDF(path, pdfPath);if (bo) {System.out.println("恭喜您,转换完成!");} else {System.out.println("转换失败!");}}// word转换为PDFpublic static boolean wordtoPDF(String inputFile, String pdfFile) {try {// 打开word应用程序ActiveXComponent app = new ActiveXComponent("Word.Application");// 设置word不可见app.setProperty("Visible", false);// 获得word中所有打开的文档,返回Documents对象Dispatch docs = app.getProperty("Documents").toDispatch();// 调用Documents对象中Open方法打开文档,并返回打开的文档对象DocumentDispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();// 调用Document对象的SaveAs方法,将文档保存为pdf格式Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17// 关闭文档Dispatch.call(doc, "Close", false);// 关闭word应用程序app.invoke("Quit", 0);return true;} catch (Exception e) {return false;}}// excel转换为PDFpublic static boolean exceltoPDF(String inputFile, String pdfFile) {try {ActiveXComponent app = new ActiveXComponent("Excel.Application");app.setProperty("Visible", false);Dispatch excels = app.getProperty("Workbooks").toDispatch();Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();Dispatch.call(excel, "ExportAsFixedFormat", xlsTypePDF, pdfFile);Dispatch.call(excel, "Close", false);app.invoke("Quit");return true;} catch (Exception e) {return false;}}// PPT转换为PDFpublic static boolean ppttoPDF(String inputFile, String pdfFile) {try {//ActiveXComponent app = new ActiveXComponent("KWPP.Application");//PowerPointActiveXComponent app = new ActiveXComponent("PowerPoint.Application");Dispatch ppts = app.getProperty("Presentations").toDispatch();Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnlytrue, // Untitled指定文件是否有标题false// WithWindow指定文件是否可见).toDispatch();Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);Dispatch.call(ppt, "Close");app.invoke("Quit");return true;} catch (Exception e) {e.printStackTrace();return false;}}}

 注意:原先我不知道wps也可以转,就用的office2016 但是excel转换你需要下载office相应插件SaveAsPDFandXPS,比较麻烦,所以我就用的office  代码我是网上搜的,不知道是谁的代码我忘了,这里说声不好意思

 

 

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...