Springboot 项目集成 PageOffice V6 最简单代码

发布时间 2024-01-11 10:41:50作者: qianxi

本文描述了PageOffice产品在Springboot项目中如何集成调用。(本示例使用了Thymeleaf模板引擎)

  1. 新建Springboot项目:pageoffice6-springboot2-simple

  2. 在您项目的pom.xml中通过下面的代码引入PageOffice依赖。pageoffice.jar已发布到Maven中央仓库 (opens new window),建议使用最新版本。

如果使用Tomcat10及以上版本,使用下面的pom.xml配置

<dependency>
  <groupId>com.zhuozhengsoft</groupId>
  <artifactId>pageoffice</artifactId>
  <version>6.0.0.11</version>
</dependency>

如果使用Tomcat9及以下的版本,使用下面的pom.xml配置

<dependency>
  <groupId>com.zhuozhengsoft</groupId>
  <artifactId>pageoffice</artifactId>
  <version>6.0.0.11-javax</version>
</dependency>
  1. 新建一个pageoffice文件夹,用来存放PageOffice的系统文件(如license.lic、客户端安装包等),比如windows环境下创建:D:/pageoffice,linux环境下创建:/root/pageoffice

  2. 拷贝pageoffice客户端安装程序到上一步创建的pageoffice文件夹下。

  • 客户端是windows环境:拷贝posetup_6.0.0.11.exe到pageoffice文件夹下;
  • 客户端是国产操作系统环境:拷贝对应芯片的PageOffice客户端deb安装包到pageoffice文件夹下;

PageOffice客户端安装程序下载地址:https://gitee.com/pageoffice/pageoffice6-client/releases

  1. 打开springboot-back项目的配置文件application.properties,添加一个posyspath变量,值为上一步创建的pageoffice文件夹的路径
posyspath=D:/pageoffice
  1. 在您项目的启动类Application类中添加一项@Bean配置,此为PageOffice服务器端的必要配置,代码如下:
@Value("${posyspath}")
private String poSysPath;

@Bean
public ServletRegistrationBean pageofficeRegistrationBean()  {
  com.zhuozhengsoft.pageoffice.poserver.Server poserver 
      						= new com.zhuozhengsoft.pageoffice.poserver.Server();
  poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录
    
  ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
  srb.addUrlMappings("/poserver.zz");
  srb.addUrlMappings("/poclient");
  srb.addUrlMappings("/pageoffice.js");
  srb.addUrlMappings("/sealsetup.exe");
  return srb;
}
  1. 在D盘根目录下准备一个有内容的test.docx文件,新建Controller并调用PageOffice在线打开此文件,例如SimpleWordController代码如下:
@RestController
@RequestMapping(value = "/simpleWord")
public class SimpleWordController {

  @RequestMapping(value="/openFile")
  public String openFile(HttpServletRequest request) {
      PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
      poCtrl.setSaveFilePage("saveFile");
      //webOpen的第一个参数支持能够输出下载文件的Url相对地址或者文件在服务器上的磁盘路径两种方式
      //查看详细,请在本站搜索“PageOffice属性或方法中涉及到的URL路径或磁盘路径的说明”
      poCtrl.webOpen("D:\\test.docx", OpenModeType.docNormalEdit, "张三");
      request.setAttribute("poHtmlCode", poCtrl.getHtmlCode());
      return  "simpleWord";
  }

  @RequestMapping("/saveFile")
  public void saveFile(HttpServletRequest request, HttpServletResponse response) {
      FileSaver fs = new FileSaver(request, response);
      fs.saveToFile("D:\\" + fs.getFileName());
      fs.close();
  }
}
  1. 为上一步代码中的openFile方法准备Thymeleaf模板:simpleWord.html,代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>最简单的打开保存文件</title>
</head>
<body>
<div style="width:100%; height:800px;"  th:utext="${poHtmlCode}"></div>
<script>
    function Save() {
        pageofficectrl.WebSave();
    }
    function OnPageOfficeCtrlInit() {
        pageofficectrl.AddCustomToolButton("保存", "Save", 1);
    }
</script>
</body>
</html>
  1. 在需要点击超链接实现在线打开文件的页面(比如:index.html)中添加pageoffice.js文件的引用。
<script type="text/javascript" src="/pageoffice.js"></script>
  1. 然后在页面中添加一个超链接,点击超链接调用POBrowser对象的openWindow方法,弹出新浏览器窗口访问simpleWord/openFile在线打开文件,代码如下:
<a href="javascript:POBrowser.openWindow('simpleWord/openFile','width=1150px;height=900px;');">
    在线打开文档
</a>
  1. 启动项目,点击“在线打开文档”超链接,查看在线打开编辑保存Office文件的效果。

参考链接:PageOffice最简集成代码(Springboot)