java 預覽word,ppt,xls等office檔案技術實現
阿新 • • 發佈:2019-01-06
網上找了很多相關文件,也爬過很多坑,現在基於最新版本的軟體(OpenOffice 4.1.3)和jar(Jodconverter Core--4.0.0-RELEASE)
包整理出一份可以實現office檔案預覽功能,供參考。
1.pdf檔案預覽
參考pdf js官網:pdf預覽例子
準備一個pdf檔案,pdf.js,pdf.worker.js(從官網可以下載)
預覽程式碼pdftest.html(從官網copy後做微小改動)必須放到容器中才能預覽,tomcat,apache都行
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pdf預覽</title>
<!-- <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script> -->
<!-- 改成引用本地js-->
<script src="js/pdf.js"></script>
<div style="margin: auto;width: 500px;">
<a href="javascript:void(0)" id="prev">上一頁</a >
<a href="javascript:void(0)" id="next">下一頁</a>
<span>頁碼: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
<div style="margin: auto;width: 600px;">
<canvas style="width: 100%;" id="the-canvas" ></canvas>
</div>
</head>
<body>
<script type="text/javascript">
//預覽測試檔案
var url = 'Paper.pdf';
// 改成引用本地js
PDFJS.workerSrc = 'js/pdf.worker.js';
//PDFJS.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
function renderPage(num) {
pageRendering = true;
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
// Wait for rendering to finish
renderTask.promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
// New page rendering is pending
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
// Update page counters
document.getElementById('page_num').textContent = pageNum;
}
/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
pageNumPending = num;
} else {
renderPage(num);
}
}
/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById('prev').addEventListener('click', onPrevPage);
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);
/**
* Asynchronously downloads PDF.
*/
PDFJS.getDocument(url).then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
</script>
</body>
</html>
預覽效果截圖
2.office文件轉pdf
2.1安裝openoffice
openoffice官網:下載地址
2.2引入jar包
maven jar地址:jodconverter-core
如果是maven專案直接引用依賴即可
<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.0.0-RELEASE</version>
</dependency>
如果是web專案需下載其依賴包
2.3 java將office檔案轉換成pdf檔案
直接粘原始碼
import java.io.File;
import java.util.Date;
import java.util.regex.Pattern;
import org.jodconverter.OfficeDocumentConverter;
import org.jodconverter.office.DefaultOfficeManagerBuilder;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
public class Test {
public static void word2pdf(String inputFilePath) {
System.out.println("inputFilePath==="+inputFilePath);
String outputFilePath = getOutputFilePath(inputFilePath);
File inputFile = new File(inputFilePath);
if (inputFile.exists()) {// 找不到原始檔, 則不啟動
DefaultOfficeManagerBuilder config = new DefaultOfficeManagerBuilder();
String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);
OfficeManager officeManager = config.build();
try {
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File outputFile = new File(outputFilePath);
if (!outputFile.getParentFile().exists()) { // 假如目標路徑不存在, 則新建該路徑
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
officeManager.stop();
} catch (OfficeException e) {
e.printStackTrace();
}
}
}
//如果不輸出在同目錄,自行構造outputFilePath路徑
public static String getOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll(".[a-zA-Z]*$", ".pdf");
return outputFilePath;
}
public static String getOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice.org3";
} else if (Pattern.matches("Windows.*", osName)) {
//openoffice安裝路徑
return "C:/Program Files (x86)/OpenOffice 4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}
public static void main(String[] args) throws Exception {
Date d1 = new Date();
Test.word2pdf("d:/log/w.doc");
Date d2 = new Date();
System.out.println(d2.getTime()-d1.getTime());
// Test.word2pdf("d:/log/a.pptx");
// Test.word2pdf("d:/log/w.xlsx");
}
}
好了,office轉pdf,pdf預覽都具備,office檔案預覽就是將其轉換成pdf進行預覽檢視。
相關原始碼去下面git地址檢視
springMvc