java 如何将 word,excel,ppt如何转pdf--jacob

时间:2023-11-15 13:23:02
问题:java 如果将 word,excel,ppt如何转pdf
我个人的观点:windows server下用 jacob; linux server下 用openoffice。
PS:1.本文中说的是windows server下用 jacob这一部分,之后说openoffice的解决方案。
   2.本文中说的解决方案支持将doc,docx,xls,xlsx,ppt,pptx这些格式转成pdf。
   3. jacob可以调用windows com组件,但不是不止这一种方案:可以搜“java调用com”,查看其它的解决方案
1.寻找解决方案的过程:
  项目中遇到了office(word,excel,ppt,之后统称mssoft document)转pdf的需求。
  最初借助java调用openoffice (OpenOffice.org 是一套跨平台的办公室软件套件)服务将mssoft document 转换成pdf,均能转换成功,当时的心情也是蛮激动的,感觉openoffice简直是万能啊,既能转mssoft document,又能转odt,ods,odp。但是,后来有部分mssoft document 转成pdf后,出现排版错位问题,字体莫名加粗问题,甚至有的的内容直接就消失了,苦苦冥思不得其解。
  后来在github上找了一个转pdf的项目,大致试着转了几个word,发现转pdf后的效果还说的过去,好景不长,后续的mssoft document转换到pdf后,还是出现了出现排版错位问题,这让我很抓狂。
  想想也是,mssoft document是微软的,让其他第三方的工具解析转换肯定不能达到100%效果,所以考虑能否让msoffice自己做转换操作,这样就能保证转换后的效果了。并且服务器是windows server,然后就找到了jacob(java com bridge)。
2.前提条件:
  本机运行只需要是windows系统即可。若是web项目中用到的服务器必须是windows server。原因:jacob需要借助 msoffice软件做pdf转换操作,因此需要在服务器上安装msoffice 07/10/13。
  我本机的环境:
  jdk 1.6
  tomcat 1.6
  maven 3.1 (也可以不用maven,直接创建普通的java项目也可以)
  msoffice 2010
3.准备工作:
  下载jacob.zip ,地址:https://sourceforge.net/projects/jacob-project/
java 如何将 word,excel,ppt如何转pdf--jacob
  点击图中的download即可,下载后的压缩包中有如下内容:
java 如何将 word,excel,ppt如何转pdf--jacob

  64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件放入放入jdk/bin目录下,如下图所示:

java 如何将 word,excel,ppt如何转pdf--jacob

  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"); } }