jacob java excel_Java使用jacob将微软office中word、excel、ppt转成pdf
本文实例为大家分享了Java使用jacob将微软office文档转成pdf的具体代码,供大家参考,具体内容如下
在使用jacb前,我们需要去下载 jacob.jar 和 jacob-1.18-x64.dll
其次,我们需要将jacob-1.18-x64.dll放入到jdk的bin目录下才可以使用
第三,使用jacb之前,我们需要确保office能正常使用
如果你现在使用的是maven工程,那么不好意思,现在还没有发布正式的jacb资源文件,我们需要自定的maven依赖,如下:
com.jacob
jacob
1.7
system
${basedir}/../fileConvertApp/src/main/webapp/WEB-INF/lib/jacob.jar
然后需要注意的是jar的地址,需要根据自己的情况修改
接下来我们贴一下具体使用的代码片段
import java.io.File;
import org.apache.log4j.Logger;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* Converter Util
*
* @author Jason
*
*/
public class OfficeConverterUtil {
/**
* log
*/
private static Logger logger = Logger.getLogger(OfficeConverterUtil.class);
private static final int WDFO_RMATPDF = 17;
private static final int XLTYPE_PDF = 0;
private static final int PPT_SAVEAS_PDF = 32;
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
public static final int PPT_SAVEAS_JPG = 17;
// private static final int msoTrue = -1;
// private static final int msofalse = 0;
/**
* @param argInputFilePath
* @param argPdfPath
* @return
*/
public static boolean officeFileConverterToPdf(String argInputFilePath, String argPdfPath) {
if (argInputFilePath.isEmpty() || argPdfPath.isEmpty() || getFileSufix(argInputFilePath).isEmpty()) {
logger.debug("输入或输出文件路徑有誤!");
return false;
}
String suffix = getFileSufix(argInputFilePath);
File file = new File(argInputFilePath);
if (!file.exists()) {
logger.debug("文件不存在!");
return false;
}
// PDF如果不存在则创建文件夹
file = new File(getFilePath(argPdfPath));
if (!file.exists()) {
file.mkdir();
}
// 如果输入的路径为PDF 则生成失败
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
return false;
}
if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
return wordToPDF(argInputFilePath, argPdfPath);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excelToPdf(argInputFilePath, argPdfPath);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return pptToPdf(argInputFilePath, argPdfPath);
// return ppt2PDF(argInputFilePath, argPdfPath);
}
return false;
}
/**
* converter word to pdf
*
* @param wordPath
* @param pdfPath
* @return
*/
public static boolean wordToPDF(String wordPath, String pdfPath) {
ActiveXComponent msWordApp = new ActiveXComponent("Word.Application");
msWordApp.setProperty("Visible", new Variant(false));
Dispatch docs = Dispatch.get(msWordApp, "Documents").toDispatch();
// long pdfStart = System.currentTimeMillis();
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { wordPath, new Variant(false), new Variant(true) }, new int[1]).toDispatch();
deletePdf(pdfPath);
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { pdfPath, new Variant(WDFO_RMATPDF) }, new int[1]);
// long pdfEnd = System.currentTimeMillis();
logger.debug(wordPath + ",pdf转换完成..");
if (null != doc) {
Dispatch.call(doc, "Close", false);
}
return true;
}
/**
* excel to pdf
*
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean excelToPdf(String inputFile, String pdfFile) {
ActiveXComponent activeXComponent = new ActiveXComponent("Excel.Application");
activeXComponent.setProperty("Visible", false);
deletePdf(pdfFile);
Dispatch excels = activeXComponent.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfFile);
Dispatch.call(excel, "Close", false);
activeXComponent.invoke("Quit");
return true;
}
/**
* ppt to pdf
*
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean pptToPdf(String inputFile, String pdfFile) {
// ComThread.InitSTA();
ActiveXComponent activeXComponent = new ActiveXComponent("PowerPoint.Application");
// activeXComponent.setProperty("Visible", new Variant(false));
Dispatch ppts = activeXComponent.getProperty("Presentations").toDispatch();
deletePdf(pdfFile);
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, false, // ReadOnly
true, // Untitled指定文件是否有标题
true// WithWindow指定文件是否可见
).toDispatch();
// Dispatch ppt = Dispatch.invoke(ppts, "Open", Dispatch.Method, new Object[] { inputFile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();
// Dispatch.call(ppt, "SaveAs", pdfFile, PPT_SAVEAS_PDF);
// Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF));
// Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF));
// Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { pdfFile, PPT_SAVEAS_PDF }, new int[1]);
// Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { new Variant(PPT_SAVEAS_PDF) }, new int[1]);
Dispatch.callN(ppt, "SaveAs", new Variant(pdfFile));
Dispatch.call(ppt, "Close");
activeXComponent.invoke("Quit");
// ComThread.Release();
return true;
}
/**
* ppt to img
*
* @param inputFile
* @param imgFile
* @return
*/
public static boolean pptToImg(String inputFile, String imgFile) {
// 打开word应用程序
ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
// 设置word不可见,office可能有限制
// app.setProperty("Visible", false);
// 获取word中国所打开的文档,返回Documents对象
Dispatch files = app.getProperty("Presentations").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
// Dispatch.call(doc, "ExportAsFixedFormat", outputFile,
// PPT_TO_PDF);
Dispatch.call(file, "SaveAs", imgFile, PPT_SAVEAS_JPG);
// 关闭文档
// Dispatch.call(file, "Close", false);
Dispatch.call(file, "Close");
// 关闭word应用程序
// app.invoke("Quit", 0);
app.invoke("Quit");
return true;
}
/**
* get file extension
*
* @param argFilePath
* @return
*/
public static String getFileSufix(String argFilePath) {
int splitIndex = argFilePath.lastIndexOf(".");
return argFilePath.substring(splitIndex + 1);
}
/**
* subString file path
*
* @param argFilePath
* file path
* @return filePaths
*/
public static String getFilePath(String argFilePath) {
int pathIndex = argFilePath.lastIndexOf("/");
return argFilePath.substring(0, pathIndex);
}
/**
* 如果PDF存在则删除PDF
*
* @param pdfPath
*/
private static void deletePdf(String pdfPath) {
File pdfFile = new File(pdfPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
}
}
根据自己的调试,试验一下吧。
另外还有一段WPS转PDF,也贴一下,供大家参考一下
public void wps2PDF(String inputFile,String pdfFile) {
File sFile = new File(inputFile);
File tFile = new File(pdfFile);
ActiveXComponent wps = null;
try {
ComThread.InitSTA();
wps = new ActiveXComponent("wps.application");
ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(sFile.getAbsolutePath()));
doc.invoke("ExportPdf", new Variant(tFile.getAbsolutePath()));
doc.invoke("Close");
doc.safeRelease();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (wps != null) {
wps.invoke("Terminate");
wps.safeRelease();
}
ComThread.Release();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
jacob java excel_Java使用jacob将微软office中word、excel、ppt转成pdf相关推荐
- php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf
php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...
- java word excel ppt 图片转pdf
第一步将jar导入mvn库 下载地址 0积分 https://download.csdn.net/download/qq_35908944/18549670 mvn install:install-f ...
- 微软办公软件Word,Excel,PPT一些常用操作
Word 去掉红线 工具->拼写和语法->隐藏拼写错误 去掉段落标记 视图->显示段落标记(取消勾选) Excel 将CSV文件中科学计数法在excel中正常显示 建议直接看这篇文章 ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- 使用XSSFWorkbook导出excel在微软office中无法打开
今天遇到了在使用XSSFWorkbook导出excel在微软office中无法打开的情况,说这个excel文件损坏.但是在wps又没有出现这个问题,刚开始以为是因为不兼容微软office 2007以下 ...
- Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)
Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...
- Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片
将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...
- Vue 预览word,excel,ppt等office文档-内网访问(基于onlyoffice,后端返回文件流)
Vue 预览word,excel等office 先看效果!! 需求背景:在前端页面中预览office文件且是内网访问,服务器不可访问外网的前提. 因此微软的接口就废掉了,因为他接口的条件是可以访问外网 ...
- Word,Excel,PPT等Office文件Web浏览器在线预览
博主联系方式 https://fizzz.blog.csdn.net/article/details/113049879 前两天接到一个需求:需要在线预览用户上传的Word,Excel,PPT文档 ...
最新文章
- Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)
- mysql所有的编码_MySQL 批量修改数据表编码及字符集
- JUC并发编程四 并发架构--并发之共享模型
- MySql cmd下的学习笔记 —— 有关建立数据库的操作(连接Mysql,建立数据库,删除数据库等等)...
- P1971 [NOI2011]兔兔与蛋蛋游戏
- Spring Security和多个过滤器链
- vant按需引入没样式_vue vant-ui样式出不来的问题
- 不分享“年度报告”的人,多少有点难言之隐
- Android之——图片的内存优化
- 4K TEST SEQUENCES 测试视频片段下载
- 2021年高教杯数学建模国赛B题思路详解
- 计算机插u盘的接口通常是,在计算机上插u盘的接口是什么标准接口
- 关于邮箱的正则表达式
- 关于对ffmpeg中SAR/DAR/PAR的理解
- office 论文 页码_「论文页码设置」Word论文页码怎么设置连续?老师傅教你一份设置技巧(很多还不知道) - seo实验室...
- 矩阵分析之 实矩阵分解(4)满秩分解,QR分解
- java graphics2d 绘图_java GUI Graphics2D 绘图
- RAID技术图解(mdadm)
- 鸿蒙不会代替安卓,华为鸿蒙2.0可以替代安卓吗,华为鸿蒙2.0优势在哪
- Multisim10.0 软件安装教程
热门文章
- 企业运维岗位笔试真题
- 【CTF WriteUp】2020全国工业互联网安全技术技能大赛(原护网杯)Crypto题解
- 【noip2005】篝火晚会
- 成功解决:RuntimeError: implement_array_function method already has a docstring
- centos7扫描新硬盘_跟大家讲讲硬盘基础知识
- c# 傅里叶变换 频域_C# 傅里叶变换 逆变换 调用MathNet包
- 2010考研数学二第(11)题——高阶导数
- android 4.4 art模式,安卓4.4的ART模式怎么打开 安卓4.4开启art模式方法图解
- 阿里云服务器Windows系统试用和配置
- 未来五年10大关键IT趋势