簡介:
這篇文章中的代碼都是參考于網上的,只做一個記錄。主要做的就是實現一個office在線預覽功能。
第一步:裝office
第二步:下載jacob
打開網址下載,目前最新的是1.19版本。
第三步:配置jdk
解壓下載完的jacob壓縮包,根據jdk的版本選擇dll中的一個,放入/jdk/jre/bin中。
第四步:在項目中引入jar包
在maven官網上找不到com.jacob的jar包,只能手動引入,這個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轉化為pdf文件
這里需要再次說明,這個代碼不是我寫的,這里只是做個記錄,方便下次用到的時候直接使用。
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 path3 = "C:\\Users\\acer\\Desktop\\測試.pdf";
word2PDF(path, path3);
String path4 = "C:\\Users\\acer\\Desktop\\測試2.ppt";
String path5 = "C:\\Users\\acer\\Desktop\\測試2.pdf";
ppt2PDF(path4, path5);
String path6 = "C:\\Users\\acer\\Desktop\\測試3.xls";
String path7 = "C:\\Users\\acer\\Desktop\\測試3.pdf";
excel2PDF(path6, path7);
}
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) {
//從數據庫中查出文件位置和文件名字
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在線預覽</title> </head> <body> <div > <embed src="/GetPdf.do" type="application/pdf" /> </div> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。