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的地址,需要根据自己的情况修改
接下来我们贴一下具体使用的代码片段
importjava.io.File;
importorg.apache.log4j.Logger;
importcom.jacob.activeX.ActiveXComponent;
importcom.jacob.com.Dispatch;
importcom.jacob.com.Variant;
/**
*ConverterUtil
*
*@authorJason
*
*/
publicclassOfficeConverterUtil{
/**
*log
*/
privatestaticLoggerlogger=Logger.getLogger(OfficeConverterUtil.class);
privatestaticfinalintWDFO_RMATPDF=17;
privatestaticfinalintXLTYPE_PDF=0;
privatestaticfinalintPPT_SAVEAS_PDF=32;
publicstaticfinalintWORD_HTML=8;
publicstaticfinalintWORD_TXT=7;
publicstaticfinalintEXCEL_HTML=44;
publicstaticfinalintPPT_SAVEAS_JPG=17;
//privatestaticfinalintmsoTrue=-1;
//privatestaticfinalintmsofalse=0;
/**
*@paramargInputFilePath
*@paramargPdfPath
*@return
*/
publicstaticbooleanofficeFileConverterToPdf(StringargInputFilePath,StringargPdfPath){
if(argInputFilePath.isEmpty()||argPdfPath.isEmpty()||getFileSufix(argInputFilePath).isEmpty()){
logger.debug("输入或输出文件路徑有誤!");
returnfalse;
}
Stringsuffix=getFileSufix(argInputFilePath);
Filefile=newFile(argInputFilePath);
if(!file.exists()){
logger.debug("文件不存在!");
returnfalse;
}
//PDF如果不存在则创建文件夹
file=newFile(getFilePath(argPdfPath));
if(!file.exists()){
file.mkdir();
}
//如果输入的路径为PDF则生成失败
if(suffix.equals("pdf")){
System.out.println("PDFnotneedtoconvert!");
returnfalse;
}
if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){
returnwordToPDF(argInputFilePath,argPdfPath);
}elseif(suffix.equals("xls")||suffix.equals("xlsx")){
returnexcelToPdf(argInputFilePath,argPdfPath);
}elseif(suffix.equals("ppt")||suffix.equals("pptx")){
returnpptToPdf(argInputFilePath,argPdfPath);
//returnppt2PDF(argInputFilePath,argPdfPath);
}
returnfalse;
}
/**
*converterwordtopdf
*
*@paramwordPath
*@parampdfPath
*@return
*/
publicstaticbooleanwordToPDF(StringwordPath,StringpdfPath){
ActiveXComponentmsWordApp=newActiveXComponent("Word.Application");
msWordApp.setProperty("Visible",newVariant(false));
Dispatchdocs=Dispatch.get(msWordApp,"Documents").toDispatch();
//longpdfStart=System.currentTimeMillis();
Dispatchdoc=Dispatch.invoke(docs,"Open",Dispatch.Method,newObject[]{wordPath,newVariant(false),newVariant(true)},newint[1]).toDispatch();
deletePdf(pdfPath);
Dispatch.invoke(doc,"SaveAs",Dispatch.Method,newObject[]{pdfPath,newVariant(WDFO_RMATPDF)},newint[1]);
//longpdfEnd=System.currentTimeMillis();
logger.debug(wordPath+",pdf转换完成..");
if(null!=doc){
Dispatch.call(doc,"Close",false);
}
returntrue;
}
/**
*exceltopdf
*
*@paraminputFile
*@parampdfFile
*@return
*/
publicstaticbooleanexcelToPdf(StringinputFile,StringpdfFile){
ActiveXComponentactiveXComponent=newActiveXComponent("Excel.Application");
activeXComponent.setProperty("Visible",false);
deletePdf(pdfFile);
Dispatchexcels=activeXComponent.getProperty("Workbooks").toDispatch();
Dispatchexcel=Dispatch.call(excels,"Open",inputFile,false,true).toDispatch();
Dispatch.call(excel,"ExportAsFixedFormat",XLTYPE_PDF,pdfFile);
Dispatch.call(excel,"Close",false);
activeXComponent.invoke("Quit");
returntrue;
}
/**
*ppttopdf
*
*@paraminputFile
*@parampdfFile
*@return
*/
publicstaticbooleanpptToPdf(StringinputFile,StringpdfFile){
//ComThread.InitSTA();
ActiveXComponentactiveXComponent=newActiveXComponent("PowerPoint.Application");
//activeXComponent.setProperty("Visible",newVariant(false));
Dispatchppts=activeXComponent.getProperty("Presentations").toDispatch();
deletePdf(pdfFile);
Dispatchppt=Dispatch.call(ppts,"Open",inputFile,false,//ReadOnly
true,//Untitled指定文件是否有标题
true//WithWindow指定文件是否可见
).toDispatch();
//Dispatchppt=Dispatch.invoke(ppts,"Open",Dispatch.Method,newObject[]{inputFile,newVariant(false),newVariant(true)},newint[1]).toDispatch();
//Dispatch.call(ppt,"SaveAs",pdfFile,PPT_SAVEAS_PDF);
//Dispatch.call(ppt,"SaveAs",pdfFile,newVariant(PPT_SAVEAS_PDF));
//Dispatch.call(ppt,"SaveAs",pdfFile,newVariant(PPT_SAVEAS_PDF));
//Dispatch.invoke(ppt,"SaveAs",Dispatch.Method,newObject[]{pdfFile,PPT_SAVEAS_PDF},newint[1]);
//Dispatch.invoke(ppt,"SaveAs",Dispatch.Method,newObject[]{newVariant(PPT_SAVEAS_PDF)},newint[1]);
Dispatch.callN(ppt,"SaveAs",newVariant(pdfFile));
Dispatch.call(ppt,"Close");
activeXComponent.invoke("Quit");
//ComThread.Release();
returntrue;
}
/**
*ppttoimg
*
*@paraminputFile
*@paramimgFile
*@return
*/
publicstaticbooleanpptToImg(StringinputFile,StringimgFile){
//打开word应用程序
ActiveXComponentapp=newActiveXComponent("PowerPoint.Application");
//设置word不可见,office可能有限制
//app.setProperty("Visible",false);
//获取word中国所打开的文档,返回Documents对象
Dispatchfiles=app.getProperty("Presentations").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatchfile=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");
returntrue;
}
/**
*getfileextension
*
*@paramargFilePath
*@return
*/
publicstaticStringgetFileSufix(StringargFilePath){
intsplitIndex=argFilePath.lastIndexOf(".");
returnargFilePath.substring(splitIndex+1);
}
/**
*subStringfilepath
*
*@paramargFilePath
*filepath
*@returnfilePaths
*/
publicstaticStringgetFilePath(StringargFilePath){
intpathIndex=argFilePath.lastIndexOf("/");
returnargFilePath.substring(0,pathIndex);
}
/**
*如果PDF存在则删除PDF
*
*@parampdfPath
*/
privatestaticvoiddeletePdf(StringpdfPath){
FilepdfFile=newFile(pdfPath);
if(pdfFile.exists()){
pdfFile.delete();
}
}
}
根据自己的调试,试验一下吧。
另外还有一段WPS转PDF,也贴一下,供大家参考一下
publicvoidwps2PDF(StringinputFile,StringpdfFile){
FilesFile=newFile(inputFile);
FiletFile=newFile(pdfFile);
ActiveXComponentwps=null;
try{
ComThread.InitSTA();
wps=newActiveXComponent("wps.application");
ActiveXComponentdoc=wps.invokeGetComponent("Documents").invokeGetComponent("Open",newVariant(sFile.getAbsolutePath()));
doc.invoke("ExportPdf",newVariant(tFile.getAbsolutePath()));
doc.invoke("Close");
doc.safeRelease();
}catch(Exceptione){
System.out.println(e.getMessage());
}finally{
if(wps!=null){
wps.invoke("Terminate");
wps.safeRelease();
}
ComThread.Release();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语