office 文档转pdf
本地先安装 金山wps,并确保可用
工程目录

1、使用前,先执行install.bat 安装jacob 到maven本地仓库
2、复制
jacob-1.18-M2-x64.dll
jacob-1.18-M2-x86.dll
到jdk的bin目录

maven的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.xiaojf.util</groupId>
<artifactId>office-converter-util</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.18-M2</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>
工具类
package cn.xiaojf.util; import java.io.File;
import java.util.UUID; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.commons.lang3.StringUtils; /**
* office 文档转换成pdf工具类,通过jacob调用wps转换文档
* @author xiaojf 2017/12/2 8:29
*/
public class OfficeConverterUtil { private static final int WDFO_RMATPDF = 17;
private static final int XLTYPE_PDF = 0;
private static final int PPT_SAVEAS_PDF = 32;
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
public static final int PPT_SAVEAS_JPG = 17;
// private static final int msoTrue = -1;
// private static final int msofalse = 0; private static ActiveXComponent wordApp = null;
private static ActiveXComponent excelApp = null;
private static ActiveXComponent pptApp = null;
private static ActiveXComponent ppApp = null; static {
wordApp = new ActiveXComponent("Word.Application");
wordApp.setProperty("Visible", new Variant(false)); excelApp = new ActiveXComponent("Excel.Application");
excelApp.setProperty("Visible", false); pptApp = new ActiveXComponent("KWPP.Application");
ppApp = new ActiveXComponent("PowerPoint.Application");
} /**
* office文件转换成pdf,根据后缀名识别转换类型
* @param inputFilePath 输入文件绝对路径
* @param pdfPath 输出文件绝对路径
* @return 转换是否成功,true 成功,false 不成功
* @author xiaojf 2017/12/2 8:30
*/
public static boolean officeFileConverterToPdf(String inputFilePath, String pdfPath) {
if (StringUtils.isBlank(inputFilePath) || StringUtils.isBlank(pdfPath) || StringUtils.isBlank(getFileSufix(inputFilePath))) {
return false;
} String suffix = getFileSufix(inputFilePath); File file = new File(inputFilePath);
if (!file.exists()) {
return false;
} // PDF如果不存在则创建文件夹
file = new File(getFilePath(pdfPath));
if (!file.exists()) {
file.mkdir();
} // 如果输入的路径为PDF 则生成失败
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
return false;
} if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
return wordToPDF(inputFilePath, pdfPath);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excelToPdf(inputFilePath, pdfPath);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return pptToPdf(inputFilePath, pdfPath);
// return ppt2PDF(argInputFilePath, argPdfPath);
} return false;
} /**
* word 转 pdf
* @param wordPath 输入文件绝对路径
* @param pdfPath 输出文件绝对路径
* @return 转换是否成功,true 成功,false 不成功
* @author xiaojf 2017/12/2 8:30
*/
public static boolean wordToPDF(String wordPath, String pdfPath) { try {
Dispatch docs = Dispatch.get(wordApp, "Documents").toDispatch();
// long pdfStart = System.currentTimeMillis();
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[]{wordPath, new Variant(false), new Variant(true)}, new int[1]).toDispatch(); deletePdf(pdfPath); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{pdfPath, new Variant(WDFO_RMATPDF)},
new int[1]);
// long pdfEnd = System.currentTimeMillis();
if (null != doc) {
Dispatch.call(doc, "Close", false);
}
} catch (Exception e) {
e.printStackTrace(); } /*
* finally { wordApp.invoke("Quit"); }
*/
return true;
} /**
* excel 转 pdf
* @param inputFile 输入文件绝对路径
* @param pdfFile 输出文件绝对路径
* @return 转换是否成功,true 成功,false 不成功
* @author xiaojf 2017/12/2 8:30
*/
public static boolean excelToPdf(String inputFile, String pdfFile) { try { deletePdf(pdfFile); Dispatch excels = excelApp.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfFile);
Dispatch.call(excel, "Close", false);
} catch (Exception e) {
e.printStackTrace();
} /*
* finally { excelApp.invoke("Quit"); }
*/
return true;
} /**
* ppt 转 pdf
* @param inputFile 输入文件绝对路径
* @param pdfFile 输出文件绝对路径
* @return 转换是否成功,true 成功,false 不成功
* @author xiaojf 2017/12/2 8:30
*/
public static boolean pptToPdf(String inputFile, String pdfFile) {
// ComThread.InitSTA(true); try {
// app.setProperty("Visible", false);
Dispatch ppts = pptApp.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
// false, // Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{pdfFile, new Variant(PPT_SAVEAS_PDF)},
new int[1]);
Dispatch.call(ppt, "Close");
} catch (Exception e) {
e.printStackTrace();
} /*
* finally { pptApp.invoke("Quit"); }
*/ return true;
} /**
* ppt 转 图片
* @param inputFile 输入文件绝对路径
* @param imgFile 输出文件绝对路径
* @return 转换是否成功,true 成功,false 不成功
* @author xiaojf 2017/12/2 8:30
*/
public static boolean pptToImg(String inputFile, String imgFile) {
// 打开word应用程序 try {
// 设置word不可见,office可能有限制
// app.setProperty("Visible", false);
// 获取word中国所打开的文档,返回Documents对象
Dispatch files = ppApp.getProperty("Presentations").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
// Dispatch.call(doc, "ExportAsFixedFormat", outputFile,
// PPT_TO_PDF);
Dispatch.call(file, "SaveAs", imgFile, PPT_SAVEAS_JPG);
// 关闭文档
// Dispatch.call(file, "Close", false);
Dispatch.call(file, "Close");
// 关闭word应用程序
// app.invoke("Quit", 0);
} catch (Exception e) {
e.printStackTrace();
} /*
* finally { ppApp.invoke("Quit"); }
*/ return true;
} /**
* 获取文件扩展名
* @param filePath
* @author xiaojf 2017/12/2 8:35
*/
public static String getFileSufix(String filePath) {
int splitIndex = filePath.lastIndexOf(".");
return filePath.substring(splitIndex + 1);
} /**
* 获取文件路径
* @param filePath
* @author xiaojf 2017/12/2 8:37
*/
public static String getFilePath(String filePath) {
int pathIndex = filePath.lastIndexOf("/");
return filePath.substring(0, pathIndex);
} /**
* 如果目标文件存在,则删除
* @param pdfPath
* @author xiaojf 2017/12/2 8:34
*/
private static void deletePdf(String pdfPath) {
File pdfFile = new File(pdfPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
} public static void main(String[] args) {
OfficeConverterUtil.wordToPDF("D:\\word2pdf\\1.doc",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".doc.pdf");
OfficeConverterUtil.wordToPDF("D:\\word2pdf\\1.docx",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".docx.pdf");
OfficeConverterUtil.pptToPdf("D:\\word2pdf\\1.ppt",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".ppt.pdf");
OfficeConverterUtil.pptToPdf("D:\\word2pdf\\1.pptx",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".pptx.pdf");
OfficeConverterUtil.wordToPDF("D:\\word2pdf\\1.rtf",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".rtf.pdf");
OfficeConverterUtil.wordToPDF("D:\\word2pdf\\1.txt",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".txt.pdf");
OfficeConverterUtil.excelToPdf("D:\\word2pdf\\1.xls",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".xls.pdf");
OfficeConverterUtil.excelToPdf("D:\\word2pdf\\1.xlsx",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".xlsx.pdf"); OfficeConverterUtil.pptToImg("D:\\word2pdf\\1.ppt",
"D:\\word2pdf\\pdf\\" + UUID.randomUUID().toString() + ".ppt.jpg"); wordApp.invoke("Quit");
wordApp.safeRelease();
excelApp.invoke("Quit");
excelApp.safeRelease();
pptApp.invoke("Quit");
pptApp.safeRelease();
ppApp.invoke("Quit");
ppApp.safeRelease(); } }
转前

转后

源码下载
https://gitee.com/xiaojf/office-converter-util.git
office 文档转pdf的更多相关文章
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- office文档转pdf
这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 转:C#实现office文档转换为PDF或xps的一些方法
代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...
- Java实现web在线预览office文档与pdf文档实例
https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...
- Java中几种office文档转pdf的方式
最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结 我主要尝试了三种方式:openoffice,aspose,jacob 对他们进行了大文件,小文件,在linux ...
- 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览
条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上) 实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...
- C#实现office文档转换为PDF或xps的一些方法( 转)
源博客http://blog.csdn.net/kable999/article/details/4786654 代码支持任意office格式 需要安装office 2007 还有一个office20 ...
- C#实现office文档转换为PDF格式
1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 OfficeSave ...
- linux平台的office文档转pdf(程序员的菜)
需要材料: 1. Openoffice3.4(我是32位的centos,可以根据自己的系统下载指定的openoffice软件包) 下载地址:http://sourceforge.net/projec ...
随机推荐
- Codeforces Round #271 (Div. 2) D 简单dp
D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...
- ubuntu 16 server 安装lnmp所需依赖
安装 1.nginx build-essential libc6 libpcre3 libpcre3-dev libssl-dev zliblg zliblg-dev lab-base 依赖库: ap ...
- ftp server安装与配置
http://note.youdao.com/noteshare?id=905513cfcdba7d6a8d2fbdd0874a6259
- 基于OpenResty和Node.js的微服务架构实践
什么是微服务? 传统的单体服务架构是单独服务包,共享代码与数据,开发成本较高,可维护性.伸缩性较差,技术转型.跨语言配合相对困难.而微服务架构强调一个服务负责一项业务,服务可以单独部署,独立进行技术选 ...
- bootstrap datetimepicker的参数解释
使用bootstrap datetimepicker(日期时间选择器)的过程中,发现中文参数说明和英文参数说明严重不符,所以结合自己使用的情况和英文参数说明,做了如下翻译. $(".form ...
- dhtmlx3.6 grid列头合并
mygrid.setHeader("序号,操作人员,发药,#cspan,审核,#cspan,数量小计,金额小计");mygrid.attachHeader(["#rspa ...
- HEXO与Github.io搭建个人博客
HEXO与Github.io搭建个人博客 HEXO搭建 HEXO是基于Node.JS的一款简单快速的博客框架,能够支持多线程,支持markdown,可以将生成的静态网页发布到github.io以 ...
- javascript小技巧之with()方法
With()方法平时用得不多,本文用个小例子来学习一下.在这里记录.个人感觉还是很方便的. 有了 With 语句,在存取对象属性和方法时就不用重复指定参考对象,在 With 语句块中,凡是 JavaS ...
- h5 canvas动画,不知道谁写的
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 2008 Round 1A C Numbers (矩阵快速幂)
题目描述: 请输出(3+√5)^n整数部分最后3位.如果结果不超过2位,请补足前导0. 分析: 我们最容易想到的方法肯定是直接计算这个表达式的值,但是这样的精度是不够的.朴素的算法没有办法得到答案.但 ...