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 ...
随机推荐
- 关于mysql engine(引擎)的疑问
http://bbs.chinaunix.net/thread-989698-1-1.html
- Retimer、Redriver(Level Shifter)
重定时器Retimer和驱动器Redriver9(Level Shifter) 在高速串行通道的信号传输中,需要使用Redriver 和Retimer来保证信号传输的质量. Redriver,可以重新 ...
- python--网络编程--主机命令执行
import os os.system()#执行系统命令 #只能执行命令不能返回值 import subprocess # 能执行系统命令 res=subprocess.Popen('dir',she ...
- C#实现网段扫描
目录1.使用的类2.获取本地主机IP地址3.远程查询4.实现网段的扫描 ---------------------------------------------------------------- ...
- ASP.NET车辆管理系统100%源代码
系统开发环境为VS2010.採用ASP.NET框架.数据库採用SQL Server.系统採用Ajax,具有:GPS导航(实时监控报警).申请审核.流程查看及短信息发送等功能.这个系统界面和功能是我认为 ...
- php 去除html标记-strip_tags和htmlspecialchars的区别
strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...
- 2809: [Apio2012]dispatching
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3102 Solved: 1641 [Sub ...
- js 中常用的正则表达式
主要有以下几种: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了哦 获取日期正则表达式:\d{4}[年|\-|\.]\d{1,2}[ ...
- [RK3288][Android6.0] 调试笔记 --- 软硬键盘同时使用【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/78748313 Platform: RK3288 OS: Android 6.0 Kernel ...
- Zookeeper集群搭建安装
三台 Linux虚拟机,每台都需要安装Jdk环境 1.上传Zookeeper安装包 (比较大 直接上传得了) 或者:wget https://mirrors.tuna.tsinghua.edu.cn/ ...