Jacob开发文件转PDF

这三种方法我都有试过word转PDF,第2种、第3种对于图片,表格、中文转换效果都不好,方法1效果最好。但方法1 只支持Windows环境下。
1.开发环境
Windows系统;
2.准备工作:
step1:下载jacob文件,里面包含jacob.jar、jacob-版本号-x64.dll,jacob-版本号-x86.dll三个文件。
step2:a.把dll文件放在%JAVA_HOME%\bin下(注意系统是32位还是64位),也可以放在C:\Windows\System32下,如果是64位应该放在C:\Windows\SysWOW64 下。建议放在jdk的bin目录下
b.如果是在eclipse下开发,需要重新引入jdk(Preference/Java/Installed JREs);
c.将jacab.jar包放在项目lib下并add到liabraries中即可.
3.代码:
Word.Excel、PPT文件转PDDF(注意自己电脑注册表是office还是WPS,决定了代码里的ActiveXComponent使用选择,我的是office,在引用资料里有使用WPS的案例)
/**
* 调用转PDF的方法
* @param inputFile 传入文件路径
* @param ouputFile 生成文件路径
* @return
*/
public static boolean PdfManager(String inputFile,String ouputFile) {
boolean flag = TransferToPDFUtil.convert2PDF(inputFile, ouputFile);
return flag;
} /***
* 判断文件类型
*
* @param fileName
* @return
*/
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
} /***
* 判断需要转化文件的类型(Excel、Word、ppt)
*
* @param inputFile 传入文件
* @param pdfFile 转化文件
*/
public static boolean convert2PDF(String inputFile, String pdfFile) {
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
System.out.println("文件不存在!");
return false;
}
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
return true;
}
if (suffix.equals("doc") || suffix.equals("docx")) {
return word2PDF(inputFile, pdfFile);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return ppt2PDF(inputFile, pdfFile);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excel2PDF(inputFile, pdfFile);
} else {
System.out.println("文件格式不支持转换!");
return false;
}
}
/***
*
* Word转PDF
*
* @param inputFile
* @param pdfFile
* @return
*/
private static boolean word2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
.toDispatch();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF
);
Dispatch.call(doc, "Close", false);
app.invoke("Quit", 0);
File fromfile = new File(inputFile);
if (fromfile.exists()) {
fromfile.delete();
}
return true;
} catch (Exception e) {
return false;
}
} private static boolean excel2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
true).toDispatch();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(excel, "ExportAsFixedFormat", xlsFormatPDF, pdfFile);
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
File fromfile = new File(inputFile);
if (fromfile.exists()) {
fromfile.delete();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} } private static boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent(
"PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(ppt, "SaveAs", pdfFile, pptFormatPDF);
Dispatch.call(ppt, "Close");
app.invoke("Quit");
File fromfile = new File(inputFile);
if (fromfile.exists()) {
fromfile.delete();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
4.引用资料:
Java使用Jacob将Word、Excel、PPT转化成PDF
Jacob开发文件转PDF的更多相关文章
- 如何通过WPS 2013 API 将Office(Word、Excel和PPT)文件转PDF文件
1. 描述 PDF 文件是一种便携文件格式,是由Adobe公司所开发的独特的跨平台文件格式.PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即 ...
- jacob 操作word转pdf
项目需要对上传的word及pdf进行在线预览,因基于jquery的pdf插件,很方面实现在线预览,而word实现在线预览费劲不少,于是想到在进行上传处理时,直接将word转成pdf,在预览时直接预览p ...
- C#写PDF文件类库PDF File Writer介绍
.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍 阅读目录 1.PDF File Writer基本介绍 2.一个简单的使用案例 3.资源 1年前,我在文章:这 ...
- PDF解决方案(2)--文件转PDF
相关专题链接: PDF解决方案(1)--文件上传 PDF解决方案(2)--文件转PDF PDF解决方案(3)--PDF转SWF PDF解决方案(4)--在线浏览 前言:上一篇中讲到的文件上传,文件上传 ...
- Apache PDFbox开发指南之PDF文档读取
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...
- Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包(免费赠送)+崔庆才
Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包+崔庆才 下载: 链接:https://pan.baidu.com/s/1H-VrvrT7wE9-CW2Dy2p0qA 提取码:35go ...
- libreoffice转换文件为pdf文件乱码问题解决办法
最近系统需要一个office文件预览功能 解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件 最后在前台使用flexpaper ...
- 执行jar文件生成pdf报错,Unsupported URL <file:///home
java -Djava.library.path=/usr/local/lib/ruby/gems/1.8/gems/sharp_office-1.0.1/ext/sigar -jar /usr/lo ...
- java 调用OpenOffice将word格式文件转换为pdf格式
一:环境搭建 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/projects/jo ...
随机推荐
- python(18)- 协程函数及应用
协程 def init(func): def wrapper(*args,**kwargs): obj = func(*args,**kwargs) next(obj) return obj retu ...
- 笔记13 winform
一:panel 和 tablelayoutpanel,FlowLayoutPanel FlowLayoutPanel和TableLayoutPanel是.NET Framework的新增控件.顾名思义 ...
- Ubuntu 16.04下配置Golang开发环境
安装之前先要明白两个变量,后面介绍安装时,会用这两个变量 GOROOT , 这是go的工作目录,比如 /home/[替换为你的用户名]/go/work GOPATH , 这是go的安装目录, ...
- 手机QQ后台清理不掉的秘密——anddroid悬浮窗
问题来自于一篇文章:手机QQ后台为何清不掉?MIUIproject师:全靠1像素的页面保命 出于好奇,想知道这一像素究竟是啥东西,用手机安全管家控制QQ的悬浮窗权限: 关闭QQ的悬浮窗权限,通过后台一 ...
- Android UI经常使用实例 怎样实现欢迎界面(Splash Screen)
在Android平台下.下载一个应用后,首次打开映入眼帘的便是Splash Screen,暂且不说Android的设计原则提不提倡这样的Splash Screen.先来看看一般使用Splash Scr ...
- iOS非常全的第三方库
iOS ● 非常全的三方库.插件.大牛博客等等 github排名:https://github.com/trending, github搜索:https://github.com/search. ...
- iOS 优化方案浅析
本文转载至 http://mobile.51cto.com/iphone-413256.htm Windows独特的注册表机制以及复杂的进程.内存管理,给了很多PC“优化”类软件极大的机遇,比如奇虎3 ...
- EasyRTMP实现RTMP异步直播推送之环形缓冲区设计
本文转自EasyDarwin团队kim的博客:http://blog.csdn.net/jinlong0603 EasyRTMP的推送缓冲区设计 EasyRTMP内部也同样采用的环形缓冲的设计方法,将 ...
- python中的括号以及元组和列表的区别
1 python中的括号 1.1 花括号 花括号表示的是字典,即键值对. 1.2 方括号 方括号表示的是列表,类似于数组,但是可以允许存放混杂类型的数据. 1.3 圆括号 圆括号表示的是元组,类似于列 ...
- 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?
作者:Wang Namelos链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...