wordexcelppt转PDF文件
Posted lvgeaibingyue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wordexcelppt转PDF文件相关的知识,希望对你有一定的参考价值。
今天项目要求做个word、excel转pdf的功能,百般搜刮,测试,终于完成.
下面这个是word、excel、ppt转pdf功能,支持doc、docx、xls、xlsx、ppt、pptx转pdf
1、 准备工作:windows环境、maven项目,普通项目也可以
下载jacob.zip,地址: https://sourceforge.net/projects/jacob-project/
2、 点击 download 就行,下载解压后显示的是 jacob-1.19文件 这个样子
3、 64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件放入放入jdk/bin目录下,如下图所示:
4、 将压缩包中的jacob.jar引入项目
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifactId> <version>1.18-M2</version> <scope>system</scope> <systemPath>D:Mavenmaven-repositoryjacobjacob.jar</systemPath> </dependency>
5、上代码
1 import com.jacob.activeX.ActiveXComponent; 2 import com.jacob.com.ComThread; 3 import com.jacob.com.Dispatch; 4 import com.jacob.com.Variant; 5 6 public class Demo { 7 private static final Integer WORD_TO_PDF_OPERAND = 17; 8 private static final Integer PPT_TO_PDF_OPERAND = 32; 9 private static final Integer EXCEL_TO_PDF_OPERAND = 0; 10 11 public void docToPdf(String srcFilePath, String pdfFilePath) throws Exception { 12 ActiveXComponent app = null; 13 Dispatch doc = null; 14 try { 15 ComThread.InitSTA(); 16 app = new ActiveXComponent("Word.Application"); 17 app.setProperty("Visible", false); 18 Dispatch docs = app.getProperty("Documents").toDispatch(); 19 Object[] obj = new Object[]{ 20 srcFilePath, 21 new Variant(false), 22 new Variant(false),//是否只读 23 new Variant(false), 24 new Variant("pwd") 25 }; 26 doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch(); 27 // Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确 28 Dispatch.put(doc, "RemovePersonalInformation", false); 29 Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17 30 31 }catch (Exception e) { 32 e.printStackTrace(); 33 throw e; 34 } finally { 35 if (doc != null) { 36 Dispatch.call(doc, "Close", false); 37 } 38 if (app != null) { 39 app.invoke("Quit", 0); 40 } 41 ComThread.Release(); 42 } 43 } 44 45 public void pptToPdf(String srcFilePath, String pdfFilePath) throws Exception { 46 ActiveXComponent app = null; 47 Dispatch ppt = null; 48 try { 49 ComThread.InitSTA(); 50 app = new ActiveXComponent("PowerPoint.Application"); 51 Dispatch ppts = app.getProperty("Presentations").toDispatch(); 52 53 /* 54 * call 55 * param 4: ReadOnly 56 * param 5: Untitled指定文件是否有标题 57 * param 6: WithWindow指定文件是否可见 58 * */ 59 ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch(); 60 Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值32 61 62 } catch (Exception e) { 63 e.printStackTrace(); 64 throw e; 65 } finally { 66 if (ppt != null) { 67 Dispatch.call(ppt, "Close"); 68 } 69 if (app != null) { 70 app.invoke("Quit"); 71 } 72 ComThread.Release(); 73 } 74 } 75 76 public void excelToPdf(String inFilePath, String outFilePath) throws Exception { 77 ActiveXComponent ax = null; 78 Dispatch excel = null; 79 try { 80 ComThread.InitSTA(); 81 ax = new ActiveXComponent("Excel.Application"); 82 ax.setProperty("Visible", new Variant(false)); 83 ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏 84 Dispatch excels = ax.getProperty("Workbooks").toDispatch(); 85 86 Object[] obj = new Object[]{ 87 inFilePath, 88 new Variant(false), 89 new Variant(false) 90 }; 91 excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); 92 93 // 转换格式 94 Object[] obj2 = new Object[]{ 95 new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0 96 outFilePath, 97 new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件 98 }; 99 Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]); 100 101 } catch (Exception es) { 102 es.printStackTrace(); 103 throw es; 104 } finally { 105 if (excel != null) { 106 Dispatch.call(excel, "Close", new Variant(false)); 107 } 108 if (ax != null) { 109 ax.invoke("Quit", new Variant[] {}); 110 ax = null; 111 } 112 ComThread.Release(); 113 } 114 115 } 116 117 public static void main(String[] args) throws Exception { 118 String path = "C:\\Users\\lihao\\Desktop\\"; 119 // new Demo().docToPdf(path + "实习报告.doc", path+ "实习报告.pdf"); 120 new Demo().excelToPdf(path + "附件2:厦门金龙联合汽车工业有限公司推广应用车辆补助资金清算信息明细表(青岛2016年第二批).xls", path+ "附件2:厦门金龙联合汽车工业有限公司推广应用车辆补助资金清算信息明细表(青岛2016年第二批).pdf"); 121 // new Demo().pptToPdf(path + "1.pptx", path + "1.pdf"); 122 123 } 124 125 126 127 }
6、本文主要参考自: https://www.cnblogs.com/xxyfhjl/p/6773786.html
以上是关于wordexcelppt转PDF文件的主要内容,如果未能解决你的问题,请参考以下文章
图文详解PDF与wordexcelppt互转工具Solid PDF Tools v9的下载安装与注册激活