java通过jacob实现office在线预览功能
简介:
这篇文章中的代码都是参考于网上的,只做一个记录。主要做的就是实现一个office在线预览功能。
第一步:装office
第二步:下载jacob
打开网址下载,目前最新的是1.19版本。
第三步:配置jdk
解压下载完的jacob压缩包,根据jdk的版本选择dll中的一个,放入/jdk/jre/bin中。
第四步:在项目中引入jar包
在maven官网上找不到com.jacob的jar包,只能手动引入,这个jar包在jacob的压缩包中有。
com.jacob jacob 1.19 system ${project.basedir}/lib/jacob.jar
第五步:将office转化为pdf文件
这里需要再次说明,这个代码不是我写的,这里只是做个记录,方便下次用到的时候直接使用。
importorg.springframework.web.bind.annotation.RequestMapping;
importjavax.servlet.http.HttpServletResponse;
importcom.jacob.activeX.ActiveXComponent;
importcom.jacob.com.ComThread;
importcom.jacob.com.Dispatch;
importcom.jacob.com.Variant;
importorg.springframework.web.bind.annotation.RestController;
importjava.io.*;
@RestController
publicclassPdfConvert{
@RequestMapping("/PdfConvert.do")
publicvoidPdfConvert(HttpServletResponseresponse){
Stringpath="C:\\Users\\acer\\Desktop\\测试.doc";
Stringpath2="C:\\Users\\acer\\Desktop\\测试.pdf";
word2PDF(path,path2);
Stringpath3="C:\\Users\\acer\\Desktop\\测试2.ppt";
Stringpath4="C:\\Users\\acer\\Desktop\\测试2.pdf";
ppt2PDF(path3,path4);
Stringpath5="C:\\Users\\acer\\Desktop\\测试3.xls";
Stringpath6="C:\\Users\\acer\\Desktop\\测试3.pdf";
excel2PDF(path5,path6);
}
publicbooleanword2PDF(StringinputFile,StringpdfFile){
ActiveXComponentapp=newActiveXComponent("Word.Application");
try{
app.setProperty("Visible",false);
Dispatchdocs=app.getProperty("Documents").toDispatch();
Dispatchdoc=Dispatch.call(docs,"Open",newObject[]{inputFile,false,true}).toDispatch();
Dispatch.call(doc,"ExportAsFixedFormat",newObject[]{pdfFile,17});
Dispatch.call(doc,"Close",newObject[]{false});
app.invoke("Quit",0);
returntrue;
}catch(Exceptionvar6){
app.invoke("Quit",0);
returnfalse;
}
}
publicbooleanexcel2PDF(StringinputFile,StringpdfFile){
ComThread.InitSTA(true);
ActiveXComponentapp=newActiveXComponent("Excel.Application");
try{
app.setProperty("Visible",false);
app.setProperty("AutomationSecurity",newVariant(3));
Dispatchexcels=app.getProperty("Workbooks").toDispatch();
Dispatchexcel=Dispatch.invoke(excels,"Open",1,newObject[]{inputFile,newVariant(false),newVariant(false)},newint[9]).toDispatch();
Dispatch.invoke(excel,"ExportAsFixedFormat",1,newObject[]{newVariant(0),pdfFile,newVariant(0)},newint[1]);
Dispatch.call(excel,"Close",newObject[]{false});
if(app!=null){
app.invoke("Quit",newVariant[0]);
app=null;
}
ComThread.Release();
returntrue;
}catch(Exceptionvar6){
app.invoke("Quit");
returnfalse;
}
}
publicbooleanppt2PDF(StringinputFile,StringpdfFile){
ActiveXComponentapp=newActiveXComponent("PowerPoint.Application");
try{
Dispatchppts=app.getProperty("Presentations").toDispatch();
Dispatchppt=Dispatch.call(ppts,"Open",newObject[]{inputFile,true,true,false}).toDispatch();
Dispatch.call(ppt,"SaveAs",newObject[]{pdfFile,32});
Dispatch.call(ppt,"Close");
app.invoke("Quit");
returntrue;
}catch(Exceptionvar6){
app.invoke("Quit");
returnfalse;
}
}
}
第六步:在页面上展示pdf
后端:
@RequestMapping("/GetPdf.do")
publicvoidGetPdf(HttpServletResponseresponse){
//从数据库中查出文件位置和文件名字
Stringpdfpath="C:\\Users\\acer\\Desktop\\测试.pdf";
Stringpdfname="测试";
try{
Filefile=newFile(pdfpath);
if(!file.exists()){
response.getWriter().write("该文档生成pdf失败,请下载文档查看");
return;
}
InputStreamfis=newFileInputStream(pdfpath);
byte[]buffer=newbyte[1024];
response.reset();
response.addHeader("Content-Disposition","inline;filename="+java.net.URLEncoder.encode(pdfname,"UTF-8"));
response.addHeader("Content-Length",""+file.length());
response.setContentType("application/pdf");
OutputStreamtoClient=newBufferedOutputStream(response.getOutputStream());
intnbytes=0;
while((nbytes=fis.read(buffer))!=-1){
toClient.write(buffer,0,nbytes);
toClient.flush();
}
toClient.flush();
toClient.close();
fis.close();
}catch(Exceptionex){
ex.printStackTrace();
}
}
前端:
pdf在线预览