package com.fy.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
/**
* 将office转换pdf工具类
*
* 说明:运行环境要求安装ms office 2007及以上(如还不能转档,请确认office中是否有另存为pdf功能,如没有需要安装SaveAsPDFFandXPS插件),
* jdk/jre/bin目录下须有jacob.dll文件,(dll文件存放在在resources目录下)
*
*
*
*/
public class Office2PdfUtils {
protected static final Logger log = Logger.getLogger(Office2PdfUtils.class);
private static final int wdFormatPDF = 17;// word转PDF格式代码
private static final int xlTypePDF = 0;// excel转PDF格式代码
private static final int ppSaveAsPDF = 32;// ppt转PDF格式代码
/**
* 将office文件转换成pdf(支持word、excle、ppt、txt)
* @param officeFileName office文件地址,如:D:/test.doc
* @param pdfFileName 转换后的pdf文件地址,如:D:/test.pdf
* @return
*/
public static Boolean office2Pdf(String officeFileName, String pdfFileName){
try {
Boolean result = null;
String suffix = getFileSuffix(officeFileName);
if("doc".equals(suffix) || "docx".equals(suffix) || "txt".equals(suffix)){
result = word2Pdf(officeFileName, pdfFileName);
}else if("xls".equals(suffix) || "xlsx".equals(suffix)){
result = excel2Pdf(officeFileName, pdfFileName);
}else if("ppt".equals(suffix) || "pptx".equals(suffix)){
result = ppt2Pdf(officeFileName, pdfFileName);
}else{
result = false;
}
return result;
} catch (Exception e) {
// TODO: handle exception
log.error("运行环境要求安装ms office 2007及以上(如还不能转档,请确认office中是否有另存为pdf功能,如没有需要安装SaveAsPDFFandXPS插件),"
+ " jdk/jre/bin目录下须有jacob.dll文件", e);
return false;
}
}
/**
* 将office文件转换成pdf(支持word、excle、ppt、txt),在同一目录下生成同名的pdf文件
* @param officeFileName office文件地址,如:D:/test.doc
* @return
*/
public static Boolean office2Pdf(String officeFileName){
String[] str = officeFileName.split("\\.");
String pdfFileName = "";
for (int i = 0; i < str.length - 1; i++) {
pdfFileName += str[i];
}
return office2Pdf(officeFileName, pdfFileName + ".pdf");
}
/**
* word转pdf
* @param docFileName
* @param pdfFileName
* @return 错误信息
*/
private static Boolean word2Pdf(String docFileName, String pdfFileName) {
Boolean result = true;
// 打开word应用程序
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
// 设置word不可见,否则会弹出word界面
app.setProperty("Visible", false);
// 获取word中所有打开的文档,并返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中的Open方法打开文档,返回打开文档的Document对象
doc = Dispatch.call(docs, "Open", docFileName, false, true)
.toDispatch();
// pdf文件
File pdfFile = new File(pdfFileName);
// pdf文件所在目录
File pdfFileDir = pdfFile.getParentFile();
// 目录不存在,先创建目录
if (!pdfFileDir.exists()) {
pdfFileDir.mkdirs();
}
// 文件已存在,删除
if (pdfFile.exists()) {
pdfFile.delete();
}
// 调用Documents对象的ExportAsFixedFomat方法,将文档保存为pdf文件
Dispatch.call(doc, "ExportAsFixedFormat", pdfFileName, wdFormatPDF);
} catch (Exception e) {
e.printStackTrace();
log.error("[" +docFileName + "]转档失败!",e);
result = false;
} finally {
if (doc != null) {
// 关闭文档
Dispatch.call(doc, "Close", false);
}
if (app != null) {
// 关闭应用
app.invoke("Quit", 0);
}
}
return result;
}
/**
* excel转pdf
* @param xlsFileName
* @param pdfFileName
* @return 错误信息
*/
private static Boolean excel2Pdf(String xlsFileName, String pdfFileName) {
Boolean result = true;
// 打开excel应用程序
ActiveXComponent app = null;
Dispatch excel = null;
try {
app = new ActiveXComponent("Excel.Application");
// 设置excel不可见,否则会弹出excel界面
app.setProperty("Visible", false);
// 获取excel中所有打开的文档,并返回Workbooks象
Dispatch excels = app.getProperty("Workbooks").toDispatch();
// 调用Workbooks对象中的Open方法打开文档,返回打开文档的Workbook对象
excel = Dispatch.call(excels, "Open", xlsFileName, false, true)
.toDispatch();
// pdf文件
File pdfFile = new File(pdfFileName);
// pdf文件所在目录
File pdfFileDir = pdfFile.getParentFile();
// 目录不存在,先创建目录
if (!pdfFileDir.exists()) {
pdfFileDir.mkdirs();
}
// 文件已存在,删除
if (pdfFile.exists()) {
pdfFile.delete();
}
// 调用Workbook对象的ExportAsFixedFomat方法,将文档保存为pdf文件
Dispatch.call(excel, "ExportAsFixedFormat",xlTypePDF, pdfFileName);
} catch (Exception e) {
e.printStackTrace();
log.error("[" +xlsFileName + "]转档失败!",e);
result = false;
} finally {
if (excel != null) {
// 关闭文档
Dispatch.call(excel, "Close", false);
}
if (app != null) {
// 关闭应用
app.invoke("Quit");
}
}
return result;
}
/**
* ppt转pdf
* @param pptFileName
* @param pdfFileName
* @return 错误信息
*/
private static Boolean ppt2Pdf(String pptFileName, String pdfFileName) {
Boolean result = true;
// 打开ppt应用程序
ActiveXComponent app = null;
Dispatch ppt = null;
try {
app = new ActiveXComponent("PowerPoint.Application");
//app.setProperty("Visible", false);
// 获取ppt中所有打开的文档,并返回Presentations象
Dispatch ppts = app.getProperty("Presentations").toDispatch();
// 调用Presentations对象中的Open方法打开文档,返回打开文档的Presentation对象
ppt = Dispatch.call(ppts,
"Open",
pptFileName,
true,//ReadOnly
true,//指定文件是否有标题
false
).toDispatch();
// pdf文件
File pdfFile = new File(pdfFileName);
// pdf文件所在目录
File pdfFileDir = pdfFile.getParentFile();
// 目录不存在,先创建目录
if (!pdfFileDir.exists()) {
pdfFileDir.mkdirs();
}
// 文件已存在,删除
if (pdfFile.exists()) {
pdfFile.delete();
}
// 调用Presentation对象的SaveAs方法,将文档保存为pdf文件
Dispatch.call(ppt, "SaveAs", pdfFileName, ppSaveAsPDF);
} catch (Exception e) {
e.printStackTrace();
log.error("[" +pptFileName + "]转档失败!",e);
result = false;
} finally {
if (ppt != null) {
// 关闭文档
Dispatch.call(ppt, "Close");
}
if (app != null) {
// 关闭应用
app.invoke("Quit");
}
}
return result;
}
/**
* 获取文件后缀
* @param fileName
* @return
*/
public static String getFileSuffix(String fileName){
if(fileName==null || fileName.trim().equals("") || fileName.indexOf(".")<0){
return null;
}
return fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
}
}

yuefeiyang1226
- 粉丝: 1
最新资源
- 西门子PLC与触摸屏在变频恒压供水系统中的设计与仿真研究
- 永磁同步电机PMSM位置三闭环控制仿真模型的设计与研究
- 马尔可夫转移场技术在一维时序信号至二维图像转换中的应用及其实现方法
- 基于MATLAB的高级蓝色车牌识别系统:集成计算机视觉与图像处理技术实现精准识别与语音播报 车牌识别
- 基于S7-200 PLC与组态王的工业锅炉温度闭环控制系统设计与实现
- 基于A与DWA融合的MATLAB路径规划算法:提高机器人避障能力 MATLAB 专业版
- 西门子S7-1200PLC与TP700触摸屏联机仿真程序:混凝土搅拌控制之博途V16及运行效果视频(带IO表)
- 独立变桨控制与统一变桨控制的OpenFast与Simlink联合仿真模型
- 永磁同步电机(PMSM)匝间短路故障的Simulink仿真分析及应用
- 密歇根大学燃料电池仿真:Simulink建模及关键组件控制策略
- 电力系统优化:基于改进粒子群算法的微电网多目标调度模型研究
- 自动驾驶路径规划与动态避障系统的实现及实验验证 · 自动驾驶 v2.5
- DEGWO-BP算法:基于差分改进灰狼优化的BP神经网络数据回归预测Matlab程序 - MATLAB
- 基于MATLAB与CarSimPreScan联合仿真的自动驾驶路径规划与动态避障模型研究 · 自动驾驶 终极版
- 恒压供水系统:西门子Smart200+海为B-7s触摸屏控制,一拖一与一拖多模式,手机远程控制程序 经典版
- 低照度图像增强技术:七大算法解析及其Python代码实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


