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=" ...
随机推荐
- janusgraph的数据模型
janusgraph的数据模型--->参考 1.简介 janusgraph的数据模型,就是一数据结构中得图结构相似.所以janusgraph的数据schema主要定义在三个要素上:顶点,边,属性 ...
- Spring和SpringMVC总结篇
作者:肥宅兜链接:https://www.cnblogs.com/doudouxiaoye/p/5693399.html 1.为什么使用Spring ? 方便解耦,简化开发;通过Spring提供的Io ...
- update Select 从查询的结果中更新表
UPDATE tbl_a a INNER JOIN tbl_b b ON a.arg=b.argSET a.arg2=b.arg2 这个语法即可实现
- redhat quay 安装试用
最近redhat 开源了quay 容器镜像管理平台,参考官方文档跑的时候需要订阅,各种不好使,然后就自己基于源码构建了 一个镜像(使用官方的dockerfile,构建出来的太大了1.9G 以及push ...
- Test of String
1.前言 这是我出的第一套题目,话说感觉有点晚了,还是在向总安排下出的.我被安排的是字符串方面的内容,这应该相对而言是比较小众的知识点吧,但是一样的有作用的,也有很神的题目.所谓是NOIP模拟题,其实 ...
- 动态规划大合集II
1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2. ...
- 通过不断迭代,编写<通过中缀表达式,构造表达式树>的代码
今天要练习的算法是通过中缀表达式生成表达式树.中缀.前缀.后缀表达式的概念就不赘述了,学习链接:中缀.前缀.后缀表达式. 参考代码学习链接:表达式树—中缀表达式转换成后缀表达式(一). [迭代 ①]: ...
- CF1174E Ehab and the Expected GCD Problem(DP,数论)
题目大意:对于一个序列,定义它的价值是它的所有前缀的 $\gcd$ 中互不相同的数的个数.给定整数 $n$,问在 $1$ 到 $n$ 的排列中,有多少个排列的价值达到最大值.答案对 $10^9+7$ ...
- 【Codeforces】B. Div Times Mod
B. Div Times Mod time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Android Studio 之 Navigation【1.页面之间的切换】
1.创建 2个 Fragment ,下面两个include 不要勾 2.创建好 Fragment 后,打开layout中的 fragment.xml 文件,将里面默认的 textView 控件删除掉 ...