java 如何将 word,excel,ppt如何转pdf--jacob


64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件放入放入jdk/bin目录下,如下图所示:
PS:我本地的是1.18-M2版本,本文中截图压缩包中的版本是1.18,所以截图中版本有不一样的地方,这不影响程序的运行。
4.将压缩包中的jacob.jar引入项目
普通的java项目(guava工具包可以自行下载)怎么引入就不细说了。
maven项目,我本地的pom.xml是这样配置的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>indi.johnny</groupId>
<artifactId>jacob-convert</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> <dependencies> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency> <dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.18-M2</version>
<scope>system</scope>
<systemPath>E:/.m2/repository/jacob-1.18-M2/jacob.jar</systemPath>
</dependency> </dependencies>
</project>
上面的配置文件中第二个<dependency>的<systemPath>标签的值就是jacob.jar的具体路径,这个改成自己的就行了。
5.上代码
下方的代码也是参考了几位博主写的博客,稍作了整理,说来惭愧。现在参考的链接我也找不全了,博主若是看到了,可以和我说一下,我把参考链接加一下。
package indi.johnny.convert; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; public class Demo {
private static final Integer WORD_TO_PDF_OPERAND = 17;
private static final Integer PPT_TO_PDF_OPERAND = 32;
private static final Integer EXCEL_TO_PDF_OPERAND = 0; public void doc2pdf(String srcFilePath, String pdfFilePath) throws Exception {
ActiveXComponent app = null;
Dispatch doc = null;
try {
ComThread.InitSTA();
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Object[] obj = new Object[]{
srcFilePath,
new Variant(false),
new Variant(false),//是否只读
new Variant(false),
new Variant("pwd")
};
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();
// Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
Dispatch.put(doc, "RemovePersonalInformation", false);
Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17 }catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
if (app != null) {
app.invoke("Quit", 0);
}
ComThread.Release();
}
} public void ppt2pdf(String srcFilePath, String pdfFilePath) throws Exception {
ActiveXComponent app = null;
Dispatch ppt = null;
try {
ComThread.InitSTA();
app = new ActiveXComponent("PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch(); /*
* call
* param 4: ReadOnly
* param 5: Untitled指定文件是否有标题
* param 6: WithWindow指定文件是否可见
* */
ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch();
Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值32 } catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (ppt != null) {
Dispatch.call(ppt, "Close");
}
if (app != null) {
app.invoke("Quit");
}
ComThread.Release();
}
} public void excel2Pdf(String inFilePath, String outFilePath) throws Exception {
ActiveXComponent ax = null;
Dispatch excel = null;
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Excel.Application");
ax.setProperty("Visible", new Variant(false));
ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = ax.getProperty("Workbooks").toDispatch(); Object[] obj = new Object[]{
inFilePath,
new Variant(false),
new Variant(false)
};
excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); // 转换格式
Object[] obj2 = new Object[]{
new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
outFilePath,
new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件
};
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]); } catch (Exception es) {
es.printStackTrace();
throw es;
} finally {
if (excel != null) {
Dispatch.call(excel, "Close", new Variant(false));
}
if (ax != null) {
ax.invoke("Quit", new Variant[] {});
ax = null;
}
ComThread.Release();
} } public static void main(String[] args) throws Exception {
String path = "C:/Users/johnny/Desktop/文档/20170427/test/001/";
new Demo().doc2pdf(path + "1.docx", path+ "1.pdf");
// new Demo().doc2pdf(path + "1.docx", path+ "1x.pdf"); } }
java 如何将 word,excel,ppt如何转pdf--jacob的更多相关文章
- java 如何将 word,excel,ppt如何转pdf --openoffice (1)
承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...
- word,excel,ppt,txt转换为 PDF
/// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath&quo ...
- windows环境下 php 将office文件(word/excel/ppt)转化为pdf(转)
将office文件转化为pdf的方法有 1.利用openoffice提供的服务 (比较简单,但是转化的效果不太好) 2.使用office提供的服务 (注:这在windows服务器上,并且服务器上面安装 ...
- php 将office文件(word/excel/ppt)转化为pdf(windows和linux只要安装对应组件应该就行)
一.配置环境 (1)配置php.ini 添加:extension=php_com_dotnet.dll com.allow_dcom = true // 去掉号,改为true 重启环境 (2) 安装 ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- 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 ...
- Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件
Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...
随机推荐
- 前端基于react,后端基于.net core2.0的开发之路(番外篇) 后端使用T4模板,生成某些类
1.介绍 因为开发过程中,有部分类是你加一个模型,就需要去改动的,每次加非常的烦,或者有些类,你只用到了他基类的方法,但是你还必须建一个文件才能调用他基类的方法,也很烦. 这个时候,T4就非常有用了. ...
- C#要点补充
1字符串与时间的互转 DateTime.TryParse将空字符串.为null或格式不正确,则转换为的DateTime所代表的值为:0001/1/1 0:00:00,此为DateTime.MinVal ...
- 初窥Flask
初窥Flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...
- Struts2+Spring+Hibernate环境搭建
struts-2.3.20 spring-4.1.4 hibernate-4.3.8 slf4j-1.7.10 1.在MySQL中建立数据库 mysql> create database myo ...
- struts2(三)之表单参数自动封装与参数类型自动转换
前言 对struts2的使用不外乎这几点,参数自动封装,拦截器的使用,数据校验,ognl表达(值栈和actionContext的讲解),struts2的标签,struts2的国际化, struts2的 ...
- 【Java学习笔记之十二】Java8增强的工具类:Arrays的用法整理总结
本文将整理 java.util.Arrays 工具类比较常用的方法: 本文介绍的方法基于JDK 1.7 之上. 1. asList方法 @SafeVarargs public static &l ...
- bzoj:1656 [Usaco2006 Jan] The Grove 树木
Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middl ...
- BZOJ 1088: [SCOI2005]扫雷Mine【思维题,神奇的模拟+枚举】
1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3791 Solved: 2234[Submit][St ...
- C++课程设计类作业4
#include <bits/stdc++.h> using namespace std; class xiexin { public: xiexin() { weight=; grade ...
- Kruskal求最小生成树
#include<bits/stdc++.h> using namespace std; ; ; const int inf = 0x3f3f3f3f; ; typedef long lo ...