转载:用pageOffice控件实现 office word文档 编辑Word加水印的功能

发布时间 2023-07-07 09:11:34作者: 爱吃苹果皮

OA办公中,业务需要多人编辑word文档,需要加文字水印的功能。

怎么实现word文档的编辑加文字水印呢?

2 实现方法

通过pageOffice实现简单的在线打开编辑word时,
通过设置doc.getWaterMark().setText("PageOffice开发平台"); 属性,给Word文档添加文字水印。
就可以实现编辑word中增加水印的功能

3 实现过程

以java的springboot框架为例

1 集成pageOffice

https://www.zhuozhengsoft.com/dowm/

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

2 在线打开编辑word

image

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

3 通过代码实现编辑word中增加水印的功能

代码参考以下功能示例代码
image

control代码

点击查看代码
@RestController
@RequestMapping(value = "/AddWaterMark")
public class AddWaterMarkController {
    @RequestMapping(value = "/Word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置服务页面
    <span class="hljs-type">WordDocument</span> <span class="hljs-variable">doc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">WordDocument</span>();
    <span class="hljs-comment">//添加水印 ,设置水印的内容</span>
    doc.getWaterMark().setText(<span class="hljs-string">"PageOffice开发平台"</span>);
    poCtrl.setWriter(doc);
    <span class="hljs-comment">//打开Word文档</span>
    poCtrl.webOpen(<span class="hljs-string">"/doc/AddWaterMark/test.doc"</span>, OpenModeType.docNormalEdit, <span class="hljs-string">"张三"</span>);
    request.setAttribute(<span class="hljs-string">"pageoffice"</span>, poCtrl.getHtmlCode(<span class="hljs-string">"PageOfficeCtrl1"</span>));
    <span class="hljs-type">ModelAndView</span> <span class="hljs-variable">mv</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ModelAndView</span>(<span class="hljs-string">"AddWaterMark/Word"</span>);
    <span class="hljs-keyword">return</span> mv;
}

}

html代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>添加水印</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>
<body>
<!--**************   卓正 PageOffice组件 ************************-->
<div style=" width:auto; height:700px;" th:utext="${pageoffice}">
</div>
</body>
</html>

通过以上代码,可以实现编辑word中增加水印的功能

4效果图

image

文档打开后,直接显示设置的文字水印

5总结

用pageOffice控件实现 office word文档 编辑word中增加水印的功能

转载:用pageOffice控件实现 office word文档 编辑Word加水印的功能