word,excel,ppt文件如何转换为pdf格式
首先进行准备工作:
下载jacob.zip ,地址:https://sourceforge.net/projects/jacob-project/ 访问进去直接有个download 直接点击下载即可

下载完直接放到你的maven仓库中,以笔者本地为例:

然后看到那两个dll文件没,如果你的电脑是64的,那么把x64的放到你的jdk的bin文件夹下,否则是32那个..

然后在你的pom文件中加入你刚才下载的jacob文件对应的依赖,其实说白了这个文件夹下有一个jacob.jar文件需要引入到项目中,笔者直接就这么搞了
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.19</version>
<scope>system</scope>
<systemPath>C:/Users/Administrator/.m2/repository/jacob-1.19/jacob.jar</systemPath>
</dependency>
好了,准备工作做好了 开始写代码...
package com.csdl.worksmanage.utils.files.conversion; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import java.io.File; public class FileConversion { private static final Integer WORD_TO_PDF_OPERAND = 17; private static final Integer PPT_TO_PDF_OPERAND = 32; private static final Integer EXCEL_TO_PDF_OPERAND = 0; public void doc2pdf(String srcFilePath, String pdfFilePath) throws Exception { ActiveXComponent app = null; Dispatch doc = null; try { ComThread.InitSTA(); /** * 创建应用程序对象,设置参数,得到文档集合,操作一个文档之前,我们必须要创建一个应用对应, * 比如是word还是excel,设置一些文档应用的参数,得到文档集合对象, * (大家应该知道word是Documents,excel是WorkBooks) */ app = new ActiveXComponent("Word.Application"); /** * 设置应用操作是文档不在明面上显示,只在后台静默处理 */ app.setProperty("Visible", false); /** * 获得文档集合,用来操作我们需要处理的文档 */ 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); Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17 }catch (Exception e) { e.printStackTrace(); throw e; } finally { if (doc != null) { Dispatch.call(doc, "Close", false); } if (app != null) { app.invoke("Quit", 0); } ComThread.Release(); } } public 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"); } ComThread.Release(); } } public 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 es) { es.printStackTrace(); throw es; } finally { if (excel != null) { Dispatch.call(excel, "Close", new Variant(false)); } if (ax != null) { ax.invoke("Quit", new Variant[] {}); ax = null; } ComThread.Release(); } } public static void main(String[] args) throws Exception { String path = "D:/1/"; // new FileConversion().doc2pdf(path + "2018年普陀桥梁总体评分.docx", path+ "2018年普陀桥梁总体评分.pdf"); new FileConversion().excel2Pdf(path + "2.xls", path+ "2.pdf"); } }
代码里面有三个方法,分别是对应word,excel,ppt格式转换为pdf的方法..
OK,如果你的程序跑通了 那么完事大吉,但是笔者在跑这些代码时还遇到一个问题...
office2Pdf导出失败,因为此功能尚未安装。。差不多就这个意思吧 是因为你的office少安装一个插件
https://dl.pconline.com.cn/download/479330-1.html
直接下载安装即可,安装完再试试发现就OK了。。

浙公网安备 33010602011771号