OA办公中,业务需要编辑打开word文档后 执行一些js操作

怎么实现编辑打开word文档后 执行一些js操作呢?

2 实现方法

通过pageOffice实现简单的在线打开编辑word时, 通过设置 关键代码: // 设置文件打开后执行的js function poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened()");

就可以实现编辑打开word文档后 执行一些js操作。

3 实现过程

以java的springboot框架为例

1 集成pageOffice

25-1.png 从pageOffice官网 下载页面,找到springboot的集成示例,按照里面的集成明说,可以集成到自己的springboot项目中。

2 在线打开编辑word

25-2.png 可以按照这个示例首先实现最基本的打开word的方法。

3 通过代码打开word文档后 执行一些js操作

示例参考 25-3.png java代码

点击查看代码

@RestController
@RequestMapping(value = "/AfterDocOpened")
public class AfterDocOpenedController {

    @RequestMapping(value = "/Word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置服务页面
        // 设置文件打开后执行的js function
        poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened()");
        //打开Word文档
        poCtrl.webOpen("/doc/AfterDocOpened/test.doc", OpenModeType.docNormalEdit, "张三");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        ModelAndView mv = new ModelAndView("AfterDocOpened/Word");
        return mv;
    }

}


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>文件打开后触发的事件</title>

</head>
<body>
<script type="text/javascript">
    function AfterDocumentOpened() {
        // 打开文件的时候,给word中当前光标位置赋值一个文本值
        document.getElementById("PageOfficeCtrl1").Document.Application.Selection.Range.Text = "文件打开了";
    }

</script>
<form id="form1">
    Word中的"<span style=" color:Red;"> 文件打开了</span>" 是在文档打开的事件中用程序添加进去的。<br/><br/>

    <div style=" width:auto; height:700px;" th:utext="${pageoffice}">
    </div>
</form>
</body>

</html>


通过以上代码,可以实现 office 文档在线编辑Word 打开之后在页面里触发的事件

4 效果图

25-4.png 文档打开后,显示的 文件打开了 是通过AfterDocumentOpened的方法写到文档中的。

5 总结

用pageOffice控件实现 office 文档在线编辑Word 打开之后在页面里触发的事件