java 如何将 word,excel,ppt如何转pdf--jacob
64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件放入放入jdk/bin目录下,如下图所示:
PS:我本地的是1.18-M2版本,本文中截图压缩包中的版本是1.18,所以截图中版本有不一样的地方,这不影响程序的运行。
4.将压缩包中的jacob.jar引入项目
普通的java项目(guava工具包可以自行下载)怎么引入就不细说了。
maven项目,我本地的pom.xml是这样配置的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>indi.johnny</groupId> <artifactId>jacob-convert</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <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>E:/.m2/repository/jacob-1.18-M2/jacob.jar</systemPath> </dependency> </dependencies> </project>
上面的配置文件中第二个<dependency>的<systemPath>标签的值就是jacob.jar的具体路径,这个改成自己的就行了。
5.上代码
下方的代码也是参考了几位博主写的博客,稍作了整理,说来惭愧。现在参考的链接我也找不全了,博主若是看到了,可以和我说一下,我把参考链接加一下。
package indi.johnny.convert; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Demo { 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(); 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 = "C:/Users/johnny/Desktop/文档/20170427/test/001/"; new Demo().doc2pdf(path + "1.docx", path+ "1.pdf"); // new Demo().doc2pdf(path + "1.docx", path+ "1x.pdf"); } }