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格式的更多相关文章

  1. Java 使用 jacob 将 word 文档转换为 pdf 文件

    网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...

  2. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

  3. 转:C#实现office文档转换为PDF或xps的一些方法

    代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...

  4. Java中几种office文档转pdf的方式

    最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结 我主要尝试了三种方式:openoffice,aspose,jacob 对他们进行了大文件,小文件,在linux ...

  5. 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

    条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上)     实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...

  6. C#实现office文档转换为PDF或xps的一些方法( 转)

    源博客http://blog.csdn.net/kable999/article/details/4786654 代码支持任意office格式 需要安装office 2007 还有一个office20 ...

  7. 文档转换为pdf格式帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = M ...

  8. OFFICE 文档转换为html在线预览

    OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的 ...

  9. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

随机推荐

  1. HDFS的Read过程分析

    在hadoop中作为后端存储的文件系统HDFS发挥中重要的作用,HDFS是一个分布式文件系统,按照Google File System的思想开发的,针对的场景是低端服务器.写操作少而读操作多的情况.在 ...

  2. mybatis的Mapper.xml文件SQL语句BadSqlGrammarException之FUNCTION错误系列

    想必各位在开发过程中一定使用过:统计的功能,用到了很多SQL的函数,于是就直接写在Mapper文件中了: 比如: member_num,MAX(ID) AS newestLoanID,MIN (ID) ...

  3. JavaScript小练习1-控制div属性

    题目 要实现的效果如图所示:查看演示 *** 分析 乍一看还以为十分简单,就是简单的点击button时触发的函数来改变样式值,不过做到后面就开始打脸了--"重置"功能.其实要实现重 ...

  4. node多图或者单图上传

    <form id="form" enctype="multipart/form-data"> <input type="text&q ...

  5. C++快速开发样本工程的建立--建立工程

    因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...

  6. 仓位 001 998 AUFNAHME不存在(L9009)

    测试做一个物料库存561初始化时,库位是上启用了WM的.提示“C01 998 AUFNAHME 不存在”,998 库存余额的初始条目 是缺省的存储类型.用LS25在正式系统中,CO1 998下有AUF ...

  7. python3网络爬虫系统学习:第一讲 基本库urllib

    在python3中爬虫常用基本库为urllib以及requests 本文主要描述urllib的相关内容 urllib包含四个模块:requests——模拟发送请求 error——异常处理模块 pars ...

  8. 轻松解决U盘加密问题

    很多小伙伴常常会遇到这样的问题,比如说有朋友或者同事想借用你的u盘,处于人情世故你又不得不借,但是又不喜欢自己的文件被别人看到或者担心丢失或被修改,在此提供一种给u盘加密或者给u盘里的文件加密的方法. ...

  9. AvalonEdit-基于WPF的代码显示控件

    AvalonEdit是基于WPF的代码显示控件,项目地址:https://github.com/icsharpcode/AvalonEdit,支持C#,javascript,C++,XML,HTML, ...

  10. 任意flex控件导出图片

    任意flex控件导出图片   flex导出图片功能通常是: 思路1:客户端将UIComponent转化为BitmapData,再转为ByteArray,将ByteArray上传到服务端,服务端发送文件 ...