Java使用Jacob将Word、Excel、PPT转化成PDF
使用Jacob将金山WPS转化成PDF,其中WPS文字使用KWPS.Aplication、Excel表格是KET.Application、演示文档是KWPP.Application,废话不多说,直接上代码:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; /***
*
* @author create by 沙漠的波浪
*
* @date 2017年2月21日---下午2:29:27
*
*/
public class Word2PDF {
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32; public static void main(String[] args) { int time = convert2PDF("D:/17-2-17诉保通服务平台(法院).ppt", "D:/17-2-17诉保通服务平台(法院).pdf"); if (time == -4) {
System.out.println("转化失败,未知错误...");
} else if(time == -3) {
System.out.println("原文件就是PDF文件,无需转化...");
} else if (time == -2) {
System.out.println("转化失败,文件不存在...");
}else if(time == -1){
System.out.println("转化失败,请重新尝试...");
}else if (time < -4) {
System.out.println("转化失败,请重新尝试...");
}else {
System.out.println("转化成功,用时: " + time + "s...");
} } /***
* 判断需要转化文件的类型(Excel、Word、ppt)
*
* @param inputFile
* @param pdfFile
*/
private static int convert2PDF(String inputFile, String pdfFile) {
String kind = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
return -2;//文件不存在
}
if (kind.equals("pdf")) {
return -3;//原文件就是PDF文件
}
if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) {
return Word2PDF.word2PDF(inputFile, pdfFile);
}else if (kind.equals("ppt")||kind.equals("pptx")) {
return Word2PDF.ppt2PDF(inputFile, pdfFile);
}else if(kind.equals("xls")||kind.equals("xlsx")){
return Word2PDF.Ex2PDF(inputFile, pdfFile);
}else {
return -4;
}
} /***
* 判断文件类型
*
* @param fileName
* @return
*/
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
} /***
*
* Word转PDF
*
* @param inputFile
* @param pdfFile
* @return
*/ private static int word2PDF(String inputFile, String pdfFile) {
// TODO Auto-generated method stub
try {
// 打开Word应用程序
ActiveXComponent app = new ActiveXComponent("KWPS.Application");
System.out.println("开始转化Word为PDF...");
long date = new Date().getTime();
// 设置Word不可见
app.setProperty("Visible", new Variant(false));
// 禁用宏
app.setProperty("AutomationSecurity", new Variant(3));
// 获得Word中所有打开的文档,返回documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
/***
*
* 调用Document对象的SaveAs方法,将文档保存为pdf格式
*
* Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
* word保存为pdf格式宏,值为17 )
*
*/
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
System.out.println(doc);
// 关闭文档
long date2 = new Date().getTime();
int time = (int) ((date2 - date) / 1000); Dispatch.call(doc, "Close", false);
// 关闭Word应用程序
app.invoke("Quit", 0);
return time;
} catch (Exception e) {
// TODO: handle exception
return -1;
} } /***
*
* Excel转化成PDF
*
* @param inputFile
* @param pdfFile
* @return
*/
private static int Ex2PDF(String inputFile, String pdfFile) {
try { ComThread.InitSTA(true);
ActiveXComponent ax = new ActiveXComponent("KET.Application");
System.out.println("开始转化Excel为PDF...");
long date = new Date().getTime();
ax.setProperty("Visible", false);
ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = ax.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch
.invoke(excels, "Open", Dispatch.Method,
new Object[] { inputFile, new Variant(false), new Variant(false) }, new int[9])
.toDispatch();
// 转换格式
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), // PDF格式=0
pdfFile, new Variant(xlTypePDF) // 0=标准 (生成的PDF图片不会变模糊) 1=最小文件
// (生成的PDF图片糊的一塌糊涂)
}, new int[1]); // 这里放弃使用SaveAs
/*
* Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{
* outFile, new Variant(57), new Variant(false), new Variant(57),
* new Variant(57), new Variant(false), new Variant(true), new
* Variant(57), new Variant(true), new Variant(true), new
* Variant(true) },new int[1]);
*/
long date2 = new Date().getTime();
int time = (int) ((date2 - date) / 1000);
Dispatch.call(excel, "Close", new Variant(false)); if (ax != null) {
ax.invoke("Quit", new Variant[] {});
ax = null;
}
ComThread.Release();
return time;
} catch (Exception e) {
// TODO: handle exception
return -1;
}
} /***
* ppt转化成PDF
*
* @param inputFile
* @param pdfFile
* @return
*/
private static int ppt2PDF(String inputFile, String pdfFile) {
try {
ComThread.InitSTA(true);
ActiveXComponent app = new ActiveXComponent("KWPP.Application");
// app.setProperty("Visible", false);
System.out.println("开始转化PPT为PDF...");
long date = new Date().getTime();
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
// false, // Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{
pdfFile,new Variant(ppSaveAsPDF)},new int[1]);
System.out.println("PPT");
Dispatch.call(ppt, "Close");
long date2 = new Date().getTime();
int time = (int) ((date2 - date) / 1000);
app.invoke("Quit");
return time;
} catch (Exception e) {
// TODO: handle exception
return -1;
}
}
}
开发所需要的jar包和dll文件的下载地址:http://download.csdn.net/my
Java使用Jacob将Word、Excel、PPT转化成PDF的更多相关文章
- 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 如何将 word,excel,ppt如何转pdf--jacob
问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice. PS:1.本文 ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- Java使用Openoffice将word、ppt转换为PDF
最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
随机推荐
- 修复OS X的Finder中文档 打开方式中重复程序的问题
如上图,OS X在使用一段时间后,有些软件就会重复注册打开方式,对于有洁癖的人,这是难以接受的事. 不过有个命令可以很简单的把重复项给去掉. /System/Library/Frameworks/Co ...
- Regularized least-squares classification(正则化最小二乘法分类器)取代SVM
在机器学习或者是模式识别其中有一种重要的分类器叫做:SVM .这个被广泛的应用于各个领域.可是其计算的复杂度以及训练的速度是制约其在实时的计算机应用的主要原因.因此也非常非常多的算法被提出来.如SMO ...
- 全志a13开发总结
这几天因为工作的原因,開始接触全志a13芯片,本人在网上搜集了好长时间,可是网上的资料对这方面的描写叙述是很少的, 所以,仅仅能靠数据手冊还有官网上面的英文文档进行开发了,下面仅仅是开发中的非常少的一 ...
- [unity3d]unity平台的预处理
在开发中,特别是unity的跨平台中,我们常常会在各个平台游走,如安卓版,苹果版,PC版.......在此不同的平台上,有可能我们须要做不同的操作.然而我们就能够用unity的自带的平台宏定义方式来做 ...
- HDOJ How many ways?? 2157【矩阵高速幂】
How many ways? ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- c程序设计语言第一章2
练习1.13编写一个程序,打印输入中单词长度的直方图.水平方向的直方图比较容易绘制,垂直方向的直方图则要困难些 #include <stdio.h> #include <stdlib ...
- Katalon
Katalon---一款好用的selenium自动化测试插件 selenium框架是目前使用较广泛的开源自动化框架,一款好的.基于界面的录制工具对于初学者来说可以快速入门:对于老手来说可以提高开发自动 ...
- HDU 3039 Go Home
今天本来解决的很好,本来可以不聊那么结束,但是我想更完美一点,多聊几句,谁知道就聊了很长时间,很傻逼.耽误了时间! /***************************************** ...
- activity四种状态
finish() 使得activity死掉 activity 部分可见进入pause状态.全部不可见进入stop状态 .界面从死亡——运行(启动) MainAdctivity.onCreate.on ...
- Windows下VMware虚拟机使用Centos,Docker方式安装openstf的小坑
今天使用docker方式安装openstf碰到了一小坑,坑了我半天.特此记录! docker方式安装stf就不说了,网上教程一大把. 但是... 安装完之后.进入web控制界面,手机连接的好好的.但硕 ...