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=" ...
随机推荐
- 第二阶段冲刺(个人)——three
今天的个人计划:选择功能界面的选择框排版设计.使得一些选择功能当点击鼠标事件后才会出现. 昨天做了什么?测试登录功能并优化. 遇到了什么困难?一些js函数的运用不熟悉,好多借助了百度.
- 复制excel表中的数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java——判断回文
package basic; import java.util.Scanner; public class Palindrome{ public static boolean isPalindrome ...
- Debian9下安装Python3 pip
Debian9下安装Python3 pip 使用apt-get安装Python3-pip包 apt-get install python3-pip
- 填坑 bzoj3337
算是个板子题吧,就是不知道啥时候能写出来. #include<cstring> #include<iostream> #include<cctype> #inclu ...
- 面试必问:HashMap 底层实现原理
HashMap是在面试中经常会问的一点,很多时候我们仅仅只是知道HashMap他是允许键值对都是Null,并且是非线程安全的,如果在多线程的环境下使用,是很容易出现问题的. 这是我们通常在面试中会说的 ...
- c++primer(第五版) 阅读笔记
快速阅读一遍c++ primer,复习c++ 1.本书代码:http://www.informit.com/store/c-plus-plus-primer-9780321714114 2.本书结构:
- 重启nova-scheduler服务,报错Login was refused using authentication mechanism AMQPLAIN
问题描述 运行 systemctl restart openstack-nova-scheduler.service 失败,查看日志报错如下: 2019-12-22 14:52:27.426 1513 ...
- IE浏览器卡死提示是否停止运行此脚本的解决办法
IE浏览器经常卡死,报是否停止运行此脚本,严重影响使用体验,下面小编教大家怎么解决这个问题,供大家参考! 1.启动IE浏览器,点击上方菜单栏位的工具,如下图所示 2.在工具栏位选择internet选项 ...
- kali 命令
1.添加用户 如下命令是用root执行的 adduser dong 会直接在/home 目录创建 dong 目录 2.将用户划分到某个组 gpasswd -a user group 3.ifconfi ...