试用版:springboot集成pageoffice实现word在线预览可编辑

1.下载pageoffice相关jar包

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
   <version>2.1.3.RELEASE</version>
</dependency>
<dependency>
   <groupId>com.zhuozhengsoft</groupId>
   <artifactId>pageoffice</artifactId>
   <scope>system</scope>
   <systemPath>本地路径/apache-maven-3.6.0/lib/aliyun/org/pageoffice/pageoffice4.5.0.10.jar</systemPath>
</dependency>

注意:pageoffice的jar包可在官方网站上下载的zip包中找到,再放到本地

2.建立controller类,粘贴一下代码

package com.demo.worddemo.cn;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@RestController
public class WordController {


    @Value("${posyspath}")
    private String poSysPath;
    @Value("${popassword}")
    private String poPassWord;

    @RequestMapping("/home")
    public String getHome(){
        return "hello";
    }

    // 第一步:转换器直接注入
//    @Autowired
//    private DocumentConverter converter;

    @Resource
    private HttpServletResponse response;

    /** 第二种方案。pageoffice服务
     * 添加PageOffice的服务器端授权程序Servlet(必须)
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
        //设置PageOffice注册成功后,license.lic文件存放的目录
        poserver.setSysPath(poSysPath);
        ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
        srb.addUrlMappings("/poserver.zz");
        srb.addUrlMappings("/posetup.exe");
        srb.addUrlMappings("/pageoffice.js");
        srb.addUrlMappings("/jquery.min.js");
        srb.addUrlMappings("/pobstyle.css");
        srb.addUrlMappings("/sealsetup.exe");
        return srb;
        //
    }

    @RequestMapping(value="/word", method= RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map){
        //--- PageOffice的调用代码 开始 -----
        PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
        poCtrl.setServerPage("/poserver.zz");//设置授权程序servlet
        poCtrl.addCustomToolButton("保存","Save",1); //添加自定义按钮
        poCtrl.setSaveFilePage("/save");//设置保存的action
        poCtrl.webOpen("d:\\test.doc", OpenModeType.docAdmin,"张三");
        map.put("pageoffice",poCtrl.getHtmlCode("PageOfficeCtrl1"));
        //--- PageOffice的调用代码 结束 -----
        ModelAndView mv = new ModelAndView("Word");
        return mv;
    }

    @RequestMapping(value="/index", method=RequestMethod.GET)
    public ModelAndView showIndex(){
        ModelAndView mv = new ModelAndView("Index");
        return mv;
    }

    @RequestMapping("/save")
    public void saveFile(HttpServletRequest request, HttpServletResponse response){
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile("e:\\" + fs.getFileName());
        fs.close();
    }



}

注:其中所涉及的本地路径更改为自己的本地路径。

3.在application.properties中添加

posyspath=d:/lic/
popassword=111111

注:license.lic,此文件在项目启动后,访问index页面后,点击打开文件,会提示安装客户端pageoffice(这里的客户端是集成到后台jar中的,不联网可下载),客户端第一次访问需注册,注册后会自动生成license.lic到指定目录下。实际部署到服务器时。路径可配置到recource下面。如图

4.在resource下建立template包

1.建立index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Index</title>
        <script type="text/javascript" src="jquery.min.js"></script>
     	<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>

    </head>
    <body>
        <h1 th:inline="text">PageOffice 集成效果演示</h1>
        
        <a href="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');">打开文件 </a>
    </body>
</html>

2.建立word.html

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>

<div style="width:1000px;height:700px;" th:utext="${pageoffice}">nihao </div>
<script type="text/javascript">
    function Save() {
        document.getElementById("PageOfficeCtrl1").WebSave();
    }
</script>

<a href="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');">打开文件</a>

此时。demo完成,即可正常启动项目:访问路径:http://localhost:8765/index   端口可自行更改

问题1:没有license.lic文件

下载此文件。解压后,将Samples4文件放到Tomcat的webapp下正常启动。按照说明正常访问并注册。则访问http://localhost:8080/Samples4/poserver.zz 此路径即可显示license的位置

问题2:没有可用的办公软件

pageoffice对客户端要求较高。客户端需安装激活版的通用office,不支持电脑自带的office365。

问题3:想要更快的创建demo方式,源码在哪里?

http://www.zhuozhengsoft.com/dowm/

 

 

如有其他问题可以留言提问,如需要相关包,请留下邮箱。

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值