网络上已经有很多这方面的内容,在用之前也是参考了好多别人的文章,下面记录下我自己的整合过程。整个过程都比较简单:

开发环境:win8 64位系统,在2008下面部署也是一样的。

文档要求jdk的版本要1.7的某个版本以上,我用的是:java version "1.7.0_80"

其他系统和环境可以下载相应的旧版本。

我是从http://sourceforge.net/projects/jacob-project/files/jacob-project/ 这里下载最新的版本jacob-1.18-M2

一个简单的工具类:

package cn.xm.exam.utils;

import java.io.File;
import java.io.IOException; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch; /**
* word转pdf的工具
*
* @author QiaoLiQiang
* @time 2018年1月4日下午2:18:33
*/
public class Word2PdfUtil { static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式 public static void main(String[] args) throws IOException {
String source1 = "C:\\Users\\liqiang\\Desktop\\考试试卷.doc";
String target1 = "C:\\Users\\liqiang\\Desktop\\考试试卷.pdf";
Word2PdfUtil.word2pdf(source1, target1);
} /**
*
* @param source
* word路径
* @param target
* 生成的pdf路径
* @return
*/
public static boolean word2pdf(String source, String target) {
System.out.println("Word转PDF开始启动...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档:" + source);
Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
System.out.println("转换文档到PDF:" + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成,用时:" + (end - start) + "ms");
return true;
} catch (Exception e) {
System.out.println("Word转PDF出错:" + e.getMessage());
return false;
} finally {
if (app != null) {
app.invoke("Quit", wdDoNotSaveChanges);
}
}
} }

结果:

Word转PDF开始启动...
打开文档:C:\Users\liqiang\Desktop\考试试卷.doc
转换文档到PDF:C:\Users\liqiang\Desktop\考试试卷.pdf
转换完成,用时:8606ms

整个代码只需要一个jacob的jar包就可以运行了。
当然,在下载的文件里面还有个调用系统库的dll文件需要放置在jre的bin目录下:
示例:D:\Java\jdk1.7.0_67\jre\bin\jacob-1.18-M2-x64.dll
这样代码就可以实现word转pdf了。

下面附上maven的pom.xml配置,因为jacob包没在第三方仓库上面直接找到,所以需要手动上传到maven中央库,或者配置下本地路径,下面粘下本地路径的配置:

<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.18-M2</version>
<scope>system</scope>
<systemPath>C:/Users/Downloads/jacob-1.18-M2/jacob.jar</systemPath>
</dependency>

这样项目构建的时候就不会出错。
顺便提一句:在部署的服务器上面需要安装office软件,要不然转换不成功,会报错。

查看office激活剩余天数方法参考:

https://zhidao.baidu.com/question/1047689643813754659.html

在windowsServer2012服务器上安装office之后报错:VariantChangeType failed

解决办法:

Windows Vista/2012改变了COM对象默认的交互方式为“非交互”型的。Console启动本身支持应用交互,但service模式下就不行了。所以需要修改word DCOM默认的标识,改为“交互式用户”模式,即可正常调用了。

按照以下步骤修改后再测service模式下试转Word即可成功:
) 运行命令: mmc comexp.msc -
) 找到:组件服务>计算机>我的电脑>DCOM组件>Microsoft Word - 文档;
) 右键点击,选择属性,修改标识为“交互式用户”,点击“确定”;

放在服务器上也报一个错误:com.jacob.com.ComFailException: Can't co-create object解决办法

解决办法:

在使用jacob调用VB.NET写的dll时,总是报错
com.jacob.com.ComFailException: Can't co-create object
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
at com.jacob.samples.test.CallDll.JavaCallVbdll(CallDll.java:19)
at com.jacob.samples.test.CallDll.main(CallDll.java:13) 网上找到几种解决办法:
1.没有释放com线程或者干脆没有使用com线程控制。因此解决方案即:释放com线程(ComThread.Release();)。因此修改代码为
public static String JavaCallVbdll(String str){
ComThread.InitSTA();
String res="";
try {
Dispatch test = new Dispatch("TestDLL.ComClass1");
Variant result = Dispatch.call(test, "teststr", str);
res=result.toString();
}catch (Exception e) {
res="";
e.printStackTrace();
}finally {
ComThread.Release();
}
return res;
} 对不起,不成功!!!!
2.在系统的服务进程中,找到“DCom Server Process Launcher”这个服务选项,请确认这个服务是关闭着的,还是开启的。
http://babystudyjava.iteye.com/blog/1746597
不好意思,我们开着呢!!!
3.JDK与JACOB版本对应,我的JDK是1.7,JACOB是1.17,电脑是win10,都是64位的。
奔溃,各版本都试过!!!
4.jar和dll文件版本需对应,jar包是64位的,dll文件是同事开发的,所以就去询问同事给我的是什么版本的dll,同事当时不造。。。 后来在Google找到一篇帖子说在VB.NET中编译选择的平台如果是Any CPU,那么久意味着生成的dll文件是32位的。没想到我们的dll文件真的是这样编译的!这里写图片描述
如上图:将Any CPU换成x64重新编译就可以了。 如此问题就解决了!!!

最后还是不稳定,最终决定直接生成pdf文件:参考http://www.cnblogs.com/qlqwjy/p/8213989.html

附一个freemarker模板生成doc文档之后转为pdf的代码:

package cn.xm.exam.action.exam.exam;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import cn.xm.exam.bean.exam.Exampaper;
import cn.xm.exam.service.exam.examPaper.ExamPaperService;
import cn.xm.exam.utils.RemoveHtmlTag;
import cn.xm.exam.utils.Word2PdfUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jxl.common.Logger; /**
* 导出试卷 1.查出数据 2.Word 3.打开流,提供下载
*
* @author QiaoLiQiang
* @time 2017年10月31日下午10:29:51
*/
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class ExtExamPaperAction extends ActionSupport {
private Logger logger = Logger.getLogger(FindExamAction.class);
private String fileName;// 导出的Excel名称
@Autowired
private ExamPaperService examPaperService;
// 1.查数据
private String paperId; public Exampaper findPaperAllInfoById() {
Exampaper paper = null;
try {
paper = RemoveHtmlTag.removePaperTag(examPaperService.getPaperAllInfoByPaperId(paperId));
} catch (SQLException e) {
logger.error("查询试卷所有信息出错!!!", e);
}
return paper;
} // 2.写入Word
public void writeExamPaper2Word(Exampaper paper) {
// 获取路径
String path = ServletActionContext.getServletContext().getRealPath("/files/papers");
// 用于携带数据的map
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("paper", paper);
String filePath = path + "\\" + fileName + ".doc";
// Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
// 指定路径的第一种方式(根据某个类的相对路径指定)
configuration.setClassForTemplateLoading(this.getClass(), "");
// 输出文档路径及名称
File outFile = new File(filePath);
// 获取文件的父文件夹并删除文件夹下面的文件
File parentFile = outFile.getParentFile();
// 获取父文件夹下面的所有文件
File[] listFiles = parentFile.listFiles();
if (parentFile != null && parentFile.isDirectory()) {
for (File fi : listFiles) {
// 删除文件
fi.delete();
}
}
// 以utf-8的编码读取ftl文件
try {
Template t = configuration.getTemplate("paperModel.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
} catch (Exception e) {
logger.error("写入word出错!", e);
}
} // 3.打开文件的流提供下载
public InputStream getInputStream() throws Exception {
Exampaper paper = this.findPaperAllInfoById();// 查数据
this.writeExamPaper2Word(paper);// 写入数据
String path = ServletActionContext.getServletContext().getRealPath("/files/papers");
String filepath = path + "\\" + fileName + ".doc";
String destPath = path + "\\" + fileName + ".pdf";
Word2PdfUtil.word2pdf(filepath, destPath);
File file = new File(destPath);
// 只用返回一个输入流
return FileUtils.openInputStream(file);// 打开文件
} // 文件下载名
public String getDownloadFileName() {
String downloadFileName = "";
String filename = fileName + ".pdf";
try {
downloadFileName = new String(filename.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
} @Override
public String execute() throws Exception {
// 先将名字设为秒数产生唯一的名字
// this.setFileName(String.valueOf(System.currentTimeMillis()));
this.setFileName("考试试卷");
return super.execute();
} // get,set方法
public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public String getPaperId() {
return paperId;
} public void setPaperId(String paperId) {
this.paperId = paperId;
} }

其他一些参数和word,ppt,excel,jpg转pdf转换请参考其他两个链接:

http://hu437.iteye.com/blog/844350
http://blog.csdn.net/xuchaozheng/article/details/19199721

更全的:http://blog.csdn.net/catoop/article/details/43150671

其他的转换方法参考:

http://feifei.im/archives/93

一个word转html的工具类:

package cn.xm.exam.utils;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; /**
* 实现word转换为HTML
*
* @author QiaoLiQiang
* @time 2018年2月3日下午2:17:43
*/
public class Word2HtmlUtil { // 8 代表word保存成html
public static final int WORD_HTML = 8; public static void main(String[] args) {
String docfile = "C:\\Users\\liqiang\\Desktop\\sbgl.docx";
String htmlfile = "C:\\Users\\liqiang\\Desktop\\sbgl.html";
Word2HtmlUtil.wordToHtml(docfile, htmlfile);
} /**
* WORD转HTML
*
* @param docfile
* WORD文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static boolean wordToHtml(String inPath, String toPath) { // 启动word
ActiveXComponent axc = new ActiveXComponent("Word.Application"); boolean flag = false; try {
// 设置word不可见
axc.setProperty("Visible", new Variant(false)); Dispatch docs = axc.getProperty("Documents").toDispatch(); // 打开word文档
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[] { inPath, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); // 作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toPath, new Variant(8) }, new int[1]); Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
return flag; } catch (Exception e) {
e.printStackTrace();
return flag;
} finally {
axc.invoke("Quit", new Variant[] {});
}
}
}

采用jacob实现word转pdf的更多相关文章

  1. jacob 操作word转pdf

    项目需要对上传的word及pdf进行在线预览,因基于jquery的pdf插件,很方面实现在线预览,而word实现在线预览费劲不少,于是想到在进行上传处理时,直接将word转成pdf,在预览时直接预览p ...

  2. java 使用jacob把word转pdf

    一.使用前要下载必要包及文件 链接: https://pan.baidu.com/s/1nvutQxb 密码: qgpi 二.引包和dll文件 1.引包:eclipse引包就不用说了,idea引包步骤 ...

  3. java word 转 pdf

    这里使用jacob将word转pdf,使用的是jacob.jar import java.io.File;import com.jacob.activeX.ActiveXComponent;impor ...

  4. Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf

    前提条件  必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...

  5. Java 将word转为pdf jacob方式

    package com.doctopdf; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jac ...

  6. 【文件】使用jacob将word转换成pdf格式

    使用jacob将word转换成pdf格式   1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装 ...

  7. Java使用Jacob将Word、Excel、PPT转化成PDF

    使用Jacob将金山WPS转化成PDF,其中WPS文字使用KWPS.Aplication.Excel表格是KET.Application.演示文档是KWPP.Application,废话不多说,直接上 ...

  8. C#实现 word、pdf、ppt 转为图片

    office word文档.pdf文档.powerpoint幻灯片是非常常用的文档类型,在现实中经常有需求需要将它们转换成图片 -- 即将word.pdf.ppt文档的每一页转换成一张对应的图片,就像 ...

  9. word转pdf字体格式变乱的问题

    完成word转pdf的功能之后,本地测试没问题,然后发布到服务器上,就遇到了字体变乱的问题,如下: 由于我本地发布后导出没有出现同样情况,而服务器和本地的最大区别在于字体库,于是,把服务器上关于需要用 ...

随机推荐

  1. 数据库——SQL数据单表查询

    数据查询   语句格式 SELECT [ALL|DISTINCT] <目标列表达式> [,<目标列表达式>] … FROM <表或视图名>[,<表或视图名&g ...

  2. Linux内核读书笔记第二周

    什么是系统调用 简单来说,系统调用就是用户程序和硬件设备之间的桥梁.用户程序在需要的时候,通过系统调用来使用硬件设备. 系统调用的存在,有以下重要的意义: 1)用户程序通过系统调用来使用硬件,而不用关 ...

  3. linux读书笔记(5章)

    linux读书笔记(5章) 标签(空格分隔): 20135328陈都 第五章 系统调用 5.1 与内核通信 系统调用 让应用程序受限的访问硬件设备 提供创建新进程并与已有进程通信的机制 提供申请操作系 ...

  4. FuelPHP 系列(三) ------ Model 模型

    框架封装好的 model 类有几个,按需继承就好. 有:/fuel/core/classes/model/crud.php /fuel/packages/orm/classes/model.php / ...

  5. c++中冒号(:)和双冒号(::)的用法

    1.冒号(:)的用法 (1)表示机构内位域的定义(即该变量占几个bit空间) typedef struct _XXX{ unsigned char a:4; unsigned char c; } ; ...

  6. Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)

    题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是  在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...

  7. 【NOIP 2018】保卫王国(动态dp / 倍增)

    题目链接 这个$dark$题,嗯,不想说了. 法一:动态$dp$ 虽然早有听闻动态$dp$,但到最近才学,如果你了解动态$dp$,那就能很轻松做出这道题了.故利用这题在这里科普一下动态$dp$的具体内 ...

  8. 960CSS框架,之前有用过 了解下框架基本原理

    http://blog.sina.com.cn/s/blog_8173443e010160b8.html CSS框架已经出现很长时间了,关于这些框架的用处也被我们讨论了很多遍了.有人说,CSS框架不够 ...

  9. Json对象与Json字符串

  10. 洛谷_Cx的故事_解题报告_第四题70

    1.并查集求最小生成树 Code: #include <stdio.h> #include <stdlib.h>   struct node {     long x,y,c; ...