java使用jacob将office文档转换为PDF格式
jacob 包下载地址: http://sourceforge.net/projects/jacob-project/
下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录中
jacob.jar 放入到 jdk/jre/lib/ext中
jacob-1.19-x64.dll放入到 jdk/jre/bin中
实现代码如下
package com.ypr.modules.op.utils; import java.io.File; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; public class ToPDF { private static final int wdFormatPDF = 17; // PDF 格式
private static final int xlTypePDF = 0; // xls格式 public boolean toPDF(String sfileName, String toFileName) {
System.out.println("------开始转换------");
String suffix = getFileSufix(sfileName);
File file = new File(sfileName);
if (!file.exists()) {
System.out.println("文件不存在!");
return false;
}
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 word2PDF(sfileName, toFileName);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return ppt2PDF(sfileName, toFileName);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excel2PDF(sfileName, toFileName);
} else {
System.out.println("文件格式不支持转换!");
return false;
} }
//截取文件后缀方法
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
//转换word文档
public boolean word2PDF(String sfileName, String toFileName) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
boolean result = true; try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
System.out.println("打开文档..." + sfileName);
System.out.println("转换文档到 PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", toFileName, wdFormatPDF);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms."); result = true;
} catch (Exception e) {
// TODO: handle exception System.out.println("========Error:文档转换失败:" + e.getMessage());
result = false;
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
} ComThread.Release(); return result;
}
//转换excel文档
public boolean excel2PDF(String inputFile, String pdfFile) {
ActiveXComponent app = null;
Dispatch excel = null;
boolean result = true;
try { app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
System.out.println("打开文档..." + inputFile);
System.out.println("转换文档到 PDF..." + pdfFile);
result = true;
} catch (Exception e) {
result = false;
} finally {
if (excel != null) {
Dispatch.call(excel, "Close");
}
if (app != null) {
app.invoke("Quit");
}
}
return result;
}
//转换ppt文档
public boolean ppt2PDF(String srcFilePath, String pdfFilePath) {
ActiveXComponent app = null;
Dispatch ppt = null;
boolean result = true;
try {
ComThread.InitSTA();
app = new ActiveXComponent("PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch(); // 因POWER.EXE的发布规则为同步,所以设置为同步发布
ppt = Dispatch.call(ppts, "Open", srcFilePath, true, // ReadOnly
true, // Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); // ppSaveAsPDF为特定值32
System.out.println("转换文档到 PDF..." + pdfFilePath);
result = true; // set flag true;
} catch (ComFailException e) {
result = false;
} catch (Exception e) {
result = false;
} finally {
if (ppt != null) {
Dispatch.call(ppt, "Close");
}
if (app != null) {
app.invoke("Quit");
}
ComThread.Release();
}
return result;
} public static void main(String[] args) {
ToPDF d = new ToPDF();
//d.wordToPDF("E:\\poi-test\\hadoop集群搭建.docx", "E:\\poi-test\\hadoop集群搭建.pdf");
d.toPDF("E:\\poi-test\\私有云清单.xlsx", "E:\\poi-test\\私有云清单.pdf"); }
}
java使用jacob将office文档转换为PDF格式的更多相关文章
- Java 使用 jacob 将 word 文档转换为 pdf 文件
网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...
- C#实现office文档转换为PDF格式
1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 OfficeSave ...
- 转:C#实现office文档转换为PDF或xps的一些方法
代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...
- 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 ...
- 文档转换为pdf格式帮助类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = M ...
- OFFICE 文档转换为html在线预览
OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的 ...
- Java实现web在线预览office文档与pdf文档实例
https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...
随机推荐
- 大话Linux内核中锁机制之完成量、互斥量
大话Linux内核中锁机制之完成量.互斥量 在上一篇博文中笔者分析了关于信号量.读写信号量的使用及源码实现,接下来本篇博文将讨论有关完成量和互斥量的使用和一些经典问题. 八.完成量 下面讨论完成量的内 ...
- 每天to do list
至少写一页书 写代码做一个实验 读10+页专业书 一年时间,如果经济状况没有改善的话,回归企业.
- oracle json 解析函数
CREATE OR REPLACE TYPE ty_tbl_str_split IS TABLE OF ty_row_str_split;CREATE OR REPLACE TYPE ty_row_s ...
- iOS项目启动及启动时间优化
app的启动入口Main函数: int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc ...
- 第一章 程序设计和C语言(笔记)
一.程序和程序语言 程序:完成某项事务所预设的活动方式和活动过程. 程序设计:人们描述计算机要做的工作. 对于工作过程的细节动作描述就是一个“程序”. 在一个程序描述中,总有一批预先假定的“基本动作” ...
- windows7平台 nginx+python 环境搭建
参考了这篇文章,感谢原文作者:https://blog.csdn.net/foxgod/article/details/78929201 最近正在学习Python,发现除了写一点py脚本在idlex上 ...
- JavaScript入门学习(2)--进度条
<html> <style type="text/css"> #bar{width:0px; height:20px; background:#ee00ff ...
- Python爬虫系列 - 初探:爬取旅游评论
Python爬虫目前是基于requests包,下面是该包的文档,查一些资料还是比较方便. http://docs.python-requests.org/en/master/ POST发送内容格式 爬 ...
- 深入浅出MFC学习笔记 第三章 MFC六大关键技术之仿真
0:MFC类层次结构 1:MFC程序的初始化过程CWinApp::InitApplication()CMyWinApp::InitInstance()CMyFrameWnd::CMyFrameWnd( ...
- 重学Verilog(1)
1.线与.线或功能 wor module WO(A, B, C, D, WireOR) input A,B,C,D; output WireOr; wor WireOr; assign WireOr ...