这三种方法我都有试过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将doc文件转换为pdf文件的三种方法

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

java使用jacob将office转pdf

Jacob开发文件转PDF的更多相关文章

  1. 如何通过WPS 2013 API 将Office(Word、Excel和PPT)文件转PDF文件

    1. 描述 PDF 文件是一种便携文件格式,是由Adobe公司所开发的独特的跨平台文件格式.PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即 ...

  2. jacob 操作word转pdf

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

  3. C#写PDF文件类库PDF File Writer介绍

    .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍   阅读目录 1.PDF File Writer基本介绍 2.一个简单的使用案例 3.资源 1年前,我在文章:这 ...

  4. PDF解决方案(2)--文件转PDF

    相关专题链接: PDF解决方案(1)--文件上传 PDF解决方案(2)--文件转PDF PDF解决方案(3)--PDF转SWF PDF解决方案(4)--在线浏览 前言:上一篇中讲到的文件上传,文件上传 ...

  5. Apache PDFbox开发指南之PDF文档读取

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...

  6. Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包(免费赠送)+崔庆才

    Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包+崔庆才 下载: 链接:https://pan.baidu.com/s/1H-VrvrT7wE9-CW2Dy2p0qA 提取码:35go ...

  7. libreoffice转换文件为pdf文件乱码问题解决办法

    最近系统需要一个office文件预览功能 解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件 最后在前台使用flexpaper ...

  8. 执行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 ...

  9. java 调用OpenOffice将word格式文件转换为pdf格式

    一:环境搭建 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/projects/jo ...

随机推荐

  1. Laravel建站01--开发环境部署

    内容导航 安装git 安装composer 安装Laravel 既然是开发环境,就需要源代码管理.这里使用git来管理. 一:部署开发环境之前安装git 在 Linux 上安装git 如果你想在 Li ...

  2. Android 短信验证码控件

    Android 短信验证码控件,便于项目中使用统一样式,统一提示改动.个人觉得挺好用的 <span style="font-size:18px;">public cla ...

  3. 【转】安卓逆向实践5——IDA动态调试so源码

    之前的安卓逆向都是在Java层上面的,但是当前大多数App,为了安全或者效率问题,会把一些重要功能放到native层,所以这里通过例子记录一下使用IDA对so文件进行调试的过程并对要点进行总结. 一. ...

  4. session.use_cookies有什么作用,

    session.use_cookies有什么作用, @ini_set('session.use_cookies', 1);什么意思 若session.use_cookies = 1 sessionid ...

  5. Cocos2d-x3.1FileUtilsTest使用

    Cocos2d-x3.1中FileUtils的使用:本使用教程是基于HelloWorld的.仅仅需在HelloWorld的init()函数中加入例如以下代码 //头文件 #include " ...

  6. 二维码、条形码扫描——使用Google ZXing

    我在项目中用到了二维码扫描的技术,用的是Google提供的ZXing开源项目,它提供二维码和条形码的扫描.扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码. 可以 ...

  7. bjfu1332 简单动规

    挺简单的动态规划题.我用记忆化搜索打的.直接上代码: /* * Author : ben */ #include <cstdio> #include <cstdlib> #in ...

  8. EasyDarwin开源平台直播架构

    Created with Raphaël 2.1.0ClientClientEasyCMSEasyCMSEasyCameraEasyCameraEasyDarwinEasyDarwin请求设备列表设备 ...

  9. WildFly JBoss 应用程序服务器

    https://en.wikipedia.org/wiki/WildFly [实现基于面向服务的架构SOA的web应用和服务] WildFly,[1] formerly known as JBoss ...

  10. ElasticSearch(七)容错机制

    一.关于横向扩容 PUT /test_index { "settings" : { "number_of_shards" : 3, "number_o ...