java通過jacob實(shí)現(xiàn)office在線預(yù)覽功能
簡介:
這篇文章中的代碼都是參考于網(wǎng)上的,只做一個(gè)記錄。主要做的就是實(shí)現(xiàn)一個(gè)office在線預(yù)覽功能。
第一步:裝office
第二步:下載jacob
打開網(wǎng)址下載,目前最新的是1.19版本。
第三步:配置jdk
解壓下載完的jacob壓縮包,根據(jù)jdk的版本選擇dll中的一個(gè),放入/jdk/jre/bin中。
第四步:在項(xiàng)目中引入jar包
在maven官網(wǎng)上找不到com.jacob的jar包,只能手動(dòng)引入,這個(gè)jar包在jacob的壓縮包中有。
<dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifactId> <version>1.19</version> <scope>system</scope> <systemPath>${project.basedir}/lib/jacob.jar</systemPath> </dependency>
第五步:將office轉(zhuǎn)化為pdf文件
這里需要再次說明,這個(gè)代碼不是我寫的,這里只是做個(gè)記錄,方便下次用到的時(shí)候直接使用。
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletResponse; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import org.springframework.web.bind.annotation.RestController; import java.io.*; @RestController public class PdfConvert { @RequestMapping("/PdfConvert.do") public void PdfConvert(HttpServletResponse response) { String path = "C:\\Users\\acer\\Desktop\\測試.doc"; String path2 = "C:\\Users\\acer\\Desktop\\測試.pdf"; word2PDF(path, path2); String path3 = "C:\\Users\\acer\\Desktop\\測試2.ppt"; String path4 = "C:\\Users\\acer\\Desktop\\測試2.pdf"; ppt2PDF(path3, path4); String path5 = "C:\\Users\\acer\\Desktop\\測試3.xls"; String path6 = "C:\\Users\\acer\\Desktop\\測試3.pdf"; excel2PDF(path5, path6); } public boolean word2PDF(String inputFile, String pdfFile) { ActiveXComponent app = new ActiveXComponent("Word.Application"); try { app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.call(docs, "Open", new Object[]{inputFile, false, true}).toDispatch(); Dispatch.call(doc, "ExportAsFixedFormat", new Object[]{pdfFile, 17}); Dispatch.call(doc, "Close", new Object[]{false}); app.invoke("Quit", 0); return true; } catch (Exception var6) { app.invoke("Quit", 0); return false; } } public boolean excel2PDF(String inputFile, String pdfFile) { ComThread.InitSTA(true); ActiveXComponent app = new ActiveXComponent("Excel.Application"); try { app.setProperty("Visible", false); app.setProperty("AutomationSecurity", new Variant(3)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch.invoke(excels, "Open", 1, new Object[]{inputFile, new Variant(false), new Variant(false)}, new int[9]).toDispatch(); Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(0), pdfFile, new Variant(0)}, new int[1]); Dispatch.call(excel, "Close", new Object[]{false}); if (app != null) { app.invoke("Quit", new Variant[0]); app = null; } ComThread.Release(); return true; } catch (Exception var6) { app.invoke("Quit"); return false; } } public boolean ppt2PDF(String inputFile, String pdfFile) { ActiveXComponent app = new ActiveXComponent("PowerPoint.Application"); try { Dispatch ppts = app.getProperty("Presentations").toDispatch(); Dispatch ppt = Dispatch.call(ppts, "Open", new Object[]{inputFile, true, true, false}).toDispatch(); Dispatch.call(ppt, "SaveAs", new Object[]{pdfFile, 32}); Dispatch.call(ppt, "Close"); app.invoke("Quit"); return true; } catch (Exception var6) { app.invoke("Quit"); return false; } } }
第六步:在頁面上展示pdf
后端:
@RequestMapping("/GetPdf.do") public void GetPdf(HttpServletResponse response) { //從數(shù)據(jù)庫中查出文件位置和文件名字 String pdfpath = "C:\\Users\\acer\\Desktop\\測試.pdf"; String pdfname = "測試"; try { File file = new File(pdfpath); if (!file.exists()) { response.getWriter().write("該文檔生成pdf失敗,請下載文檔查看"); return; } InputStream fis = new FileInputStream(pdfpath); byte[] buffer = new byte[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"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); int nbytes = 0; while ((nbytes = fis.read(buffer)) != -1) { toClient.write(buffer, 0, nbytes); toClient.flush(); } toClient.flush(); toClient.close(); fis.close(); } catch (Exception ex) { ex.printStackTrace(); } }
前端:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>pdf在線預(yù)覽</title> </head> <body> <div style=" width: 100%; height: 100%;"> <embed src="/GetPdf.do" type="application/pdf" style="overflow: auto; width: 100%; height: 800px;" /> </div> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
初學(xué)者易上手的SSH-struts2 01環(huán)境搭建(圖文教程)
下面小編就為大家?guī)硪黄鯇W(xué)者易上手的SSH-struts2 01環(huán)境搭建(圖文教程)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Spring?JPA使用CriteriaBuilder動(dòng)態(tài)構(gòu)造查詢方式
這篇文章主要介紹了Spring?JPA使用CriteriaBuilder動(dòng)態(tài)構(gòu)造查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12IDEA導(dǎo)入外部項(xiàng)目報(bào)Error:java: 無效的目標(biāo)發(fā)行版: 11的解決方法
這篇文章主要介紹了IDEA導(dǎo)入外部項(xiàng)目報(bào)Error:java: 無效的目標(biāo)發(fā)行版: 11,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)和異步調(diào)用
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)和異步調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼
在使用Mybatis時(shí),有的時(shí)候我們可以不用定義resultMap,而是直接在<select>語句上指定resultType。這個(gè)時(shí)候其實(shí)就用到了Mybatis的結(jié)果集自動(dòng)映射,下面通過本文給大家分享Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼,一起看看吧2017-02-02java實(shí)現(xiàn)簡單超市管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單超市管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01