word,excel,ppt转pdf
第一步 需要下载jar包和jacob-1.14.3-x64.dll
* <dependency>
* <groupId>net.sf.jacob-project</groupId>
* <artifactId>jacob</artifactId>
* <version>1.14.3</version>
* </dependency>
*
第二步 把jacob-1.14.3-x64.dll 放到java/bin目录下 电脑环境 win10 office2016
支持文件类型 doc,docx,xls,xlsx,ppt,pptx
下面是源代码
package com.example.demo.xs; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger; /**
* 需要jar包和jacob-1.14.3-x64.dll
* <dependency>
* <groupId>net.sf.jacob-project</groupId>
* <artifactId>jacob</artifactId>
* <version>1.14.3</version>
* </dependency>
*
* 把jacob-1.14.3-x64.dll 放到java/bin目录下
*
* 电脑环境 win10 office2016
* @author 苑庆涛
* @create 2019-07-22 16:22
*/
public class Test {
private static Logger logger = Logger.getLogger(WordApp.class.getName());
public static void main(String[] args) { } /**
* 示例 toPdf("d:\\testold\\e3.xls","d:\\testnew\\e3.pdf","xlsx")
* @param src 文件路径
* @param tar pdf文件路径
* @param type 文件类型
*/
public static void toPdf(String src,String tar,String type){
if(type.startsWith("doc")){
wordToPdf(src,tar);
}else if(type.startsWith("xls")){
excelToPdf(src,tar);
}else if(type.startsWith("ppt")){
pptToPdf(src,tar);
}
}
public static void wordToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open",
src, false, true)
.toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "SaveAs", tar,17);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close", false);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit",0);
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
} }
} public static void excelToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Workbooks").toDispatch();
doc = Dispatch.call(docs, "Open", src, false, true)
.toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "ExportAsFixedFormat", 0, tar );
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close", false);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit");
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
}
} public static void pptToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("PowerPoint.Application");
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Presentations").toDispatch();
doc = Dispatch.call(
docs,
"Open", src, true, true,false).toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "SaveAs",tar,32);
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close");
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit");
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
}
} }
word,excel,ppt转pdf的更多相关文章
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...
- PHP 实现 word/excel/ppt 转换为 PDF
前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...
- 21.PHP实现Word/Excel/PPT转换为PDF
参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
- 在线文档转换API word,excel,ppt等在线文件转pdf、png
在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...
- java 如何将 word,excel,ppt如何转pdf --openoffice (1)
承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...
- 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)
一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...
随机推荐
- Scrapy笔记04- Selector详解
Scrapy笔记04- Selector详解 在你爬取网页的时候,最普遍的事情就是在页面源码中提取需要的数据,我们有几个库可以帮你完成这个任务: BeautifulSoup是python中一个非常流行 ...
- Django 1.11 shell中模块导入问题
django报错:django.core.exceptions.ImproperlyConfigured: 处理办法 import os os.environ['DJANGO_SETTINGS_MOD ...
- Qt常用类——QFrame类与QWidge类
QFrame与QWidget的区别: QFrame是基本控件的基类,QWidget是QFrame基类. QWidget类是所有用户界面对象的基类. Widget是用户界面的基本单元:它从窗口系统接收鼠 ...
- RE:SB的SDOISB记
Day0 到了农大 进门看见hly 和myj一起乱%一通 一本爷的气场就是强大 晚上gryz搬砖三人组出去吃饭,开心>_< 吃完饭后去试机 手速比较快,写了一下ntt,lct,sa和一些小 ...
- 【BigData】Java基础_HashSet
HashSet简介 HashSet是一个集合数据类型,具有以下三个特性: (1)可以存储过个数据对象 (2)HashSet中的数据不能重复 (3)HashSet的数据存储是无序的 HashSet的几个 ...
- mac系统中怎么打开rar/zip等压缩文件?
平常使用mac的同学们,可能经常要接受下别人发过来的rar文件,可惜的时mac os x系统默认是不能打开rar文件(不知道以后苹果会支持rar不?),那么我们该如何去解圧rar文件,接下来我将介绍. ...
- Linux+Nginx+Supervisor部署ASP.NET Core实操手册
一.课程介绍 在上一节课程<ASP.NET Core托管和部署Linux实操演练手册>中我们学过net core的部署方式多样性和灵活性.我们通过远程工具输入dotnet 程序集名称.dl ...
- 基于ZYNQ 的UART中断实验之串口写数据到DDR3中
1.参考 UG585 网络笔记 2.理论知识 参见上一次实验:基于ZYNQ 的UART中断实验 3.实验目的 练习使用UART的中断实验,并将接收到的数据写入到DDR3中. 4.实验过程 建立工程,设 ...
- Angular Material
Angular Material 的设计之美 Ng-Matero 0.3 已发布,新增 module schematic 以及 page schematic,详见 README 前言 Angula ...
- Prometheus安装部署说明
本文主要介绍了如何二进制安装Prometheus.使用 Node Exporter 采集主机信息并使用Grafana来进行图形化的展示. 1. 安装Prometheus Server Promethe ...