1、下載jar包
http://www.smzy.com/smzy/down362581.html
2.將jar包打入maven倉庫
mvn install:install-file -Dfile=D:\pageoffice4.5.0.3.jar -DgroupId=my.pageoffice -DartifactId=pageoffice -Dversion=1.0 -Dpackaging=jar
3、添加依賴
<dependency>
<groupId>my.pageoffice</groupId>
<artifactId>pageoffice</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
4、獲得licence.lic
將Samples4放入tomcat,訪問:http://localhost:8080/Samples4 ,可以看到如下:
再往下拉,看到
隨便點一個,會讓註冊,隨便填,但註冊碼需要注意,要搜與jar版本匹配的或更高版本的,如果jar是4.0,註冊碼是2.0的最後打開pageoffice時就會提示你
當前PAGEOFFICE需要獲取更高版本的授權才能正常運行。(ERROR:0X0005)
我使用的(試用版的序列號)
PageOffice V4.0 標準版試用序列號:IMTG6-BSXJ-JGZ6-3BIWM
PageOffice V4.0 專業版試用序列號:CA1XB-MF7Y-12ST-PSBP2
註冊完成後,可以在D:\apache-tomcat-8.5.43\webapps\Samples4\WEB-INF\lib看到licence.lic文件
把這個licence.lic移動到一個位置,與6、中的 posyspath 一致
5、整合 thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
6、 配置Bean
import com.zhuozhengsoft.pageoffice.poserver.Server;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description :初始化Bean
* ---------------------------------
* @Author : SG.Y
* @Date : 2019/5/9 15:40
*/
@Configuration
@ConfigurationProperties
public class BeanInitConf {
// PageOffice配置
private String posyspath ="d:/lic/";
/***
* PageOffice 註冊
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
Server poserver = new Server();
//設置PageOffice註冊成功後,license.lic文件存放的目錄
poserver.setSysPath(posyspath);
ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
// 下面是把資源文件暴露出來,必須配置,否則頁面訪問不了
srb.addUrlMappings("/poserver.zz", "/posetup.exe", "/pageoffice.js", "/jquery.min.js", "/pobstyle.css", "/sealsetup.exe");
return srb;
}
public void setPosyspath(String posyspath) {
this.posyspath = posyspath;
}
}
7、測試代碼
注意裏面的文檔、licence.lic等路徑
package com.dxl.system.controller.wordonline;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @Description :Office在線編輯、預覽API
* ---------------------------------
* @Author : SG.Y
* @Date : 2019/5/14 16:36
*/
@Controller
//@Api(value = "word",tags = {"word--接口"})
public class OfficeOnlineApi {
@GetMapping("/w")
public String showWord2(){
return "ww";
}
/**
* 在線word文檔編輯
*
* @param request
* @param map
* @return
*/
@GetMapping("/word")
public String showWord(HttpServletRequest request, Map<String, Object> map) {
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
//設置授權程序servlet
poCtrl.setServerPage("/poserver.zz");
//添加自定義按鈕
poCtrl.addCustomToolButton("保存", "Save", 1);
//保存按鈕接口地址
poCtrl.setSaveFilePage("/save");
//打開文件(打開的文件類型由OpenModeType決定,docAdmin是一個word,並且是管理員權限,如果是xls文件,則使用openModeType.xls開頭的,其他的office格式同等),最後一個參數是作者
// TODO 這裏有個坑,這裏打開的文件是本地的,地址如果寫成/結構的路徑,頁面就會找不到文件,會從http://xxxxx/G/id...去找,寫成\\就是從本地找
poCtrl.webOpen("d:\\test.docx", OpenModeType.docAdmin, "光哥");
//pageoffice 是文件的變量,前端頁面通過這個變量加載出文件
map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
//跳轉到word.html
return "word";
}
/**
* 在線ppt文檔編輯
*
* @param request
* @param map
* @return
*/
@GetMapping("/ppt")
public String showPPT(HttpServletRequest request, Map<String, Object> map) {
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
//設置授權程序servlet
poCtrl.setServerPage("/poserver.zz");
//添加自定義按鈕
poCtrl.addCustomToolButton("保存", "Save", 1);
//保存接口地址
poCtrl.setSaveFilePage("/save");
//打開文件(打開的文件類型由OpenModeType決定,docAdmin是一個word,並且是管理員權限,如果是xls文件,則使用openModeType.xls開頭的,其他的office格式同等),最後一個參數是作者
// TODO 這裏有個坑,這裏打開的文件是本地的,地址如果寫成/結構的路徑,頁面就會找不到文件,會從http://xxxxx/G/id...去找,寫成\\就是從本地找
poCtrl.webOpen("G:\\ideaProject\\springboot-demo\\online-office\\src\\main\\resources\\file\\test.pptx", OpenModeType.pptNormalEdit, "光哥");
//pageoffice 是文件的變量,前端頁面通過這個變量加載出文件
map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
//跳轉到word.html
return "ppt";
}
/**
* 保存文件接口
*
* @param request
* @param response
*/
@RequestMapping("/save")
public void saveFile(HttpServletRequest request, HttpServletResponse response) {
// 保存修改後的文件
FileSaver fs = new FileSaver(request, response);
fs.saveToFile("d:\\test.docx");
fs.close();
}
}
8、templates中寫html
word.html (注意:POBrowser.openWindowModeless第一個參數 路徑)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>在線編輯Office文件</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
</head>
<body>
<!--POBrowser打開一個窗口用來編輯文件-->
<a id="open_a" style="display: none;" onclick="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');" href="#">打開文件</a>
<!-- 頁面佈局 -->
<div style="width:100%;height:900px; align-content: center" th:utext="${pageoffice}"></div>
<script type="text/javascript">
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
</script>
<script>
$(function () {
var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1;
// 獲取瀏覽器類型
function getType() {
//判斷是否chorme瀏覽器
if (userAgent.indexOf("Chrome") > -1) {
return "Chrome";
}
//判斷是否IE瀏覽器
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}
//判斷是否Edge瀏覽器
if (userAgent.indexOf("Trident") > -1) {
return "Edge";
}
}
// POBrowser打開一個窗口用來編輯文件
function open() {
$("#open_a").click();
}
// pageOffice有很多瀏覽器都不支持,不支持則使用POBrowser打開一個窗口用來編輯文件
if (getType() != 'Edge') {
// 延遲一秒在打開,是因爲如果即時打開,可能有些文件請求還沒加載好,會檢測出客戶端未安裝pageOffice控件
setTimeout(open, 1000)
}
});
</script>
</body>
</html>
ww.html(後來又隨便測試用谷歌、火狐打開pageoffice又增加的)可以自己定義所使用html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>在線編輯Office文件</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
</head>
<body>
<a id="ddddd" onclick="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');" href="#") >POBrowser方式打開Word文檔</a><br><br><br>
</body>
</html>
9、效果
點擊:(沒安裝時會提示下載安裝)