PageOffice实现文件在线安全预览——禁止编辑、下载、复制等

发布时间 2023-03-24 16:26:17作者: qianxi

实际应用中,很多时候不仅需要在线编辑文件,还需要在线安全预览文件,要求不能复制、下载、另存到本地等情况。pageoffice可以实现文件在线安全预览,禁止:编辑、复制、粘贴、右键菜单、选择、下载、另存、F12下载、PrintScreen拷屏等操作。

一、环境

前端:vue

后端:springboot 、pageoffice5.4.0.3

二、前端

Word.vue页面

后端定义一个打开文件后执行的事件。将这个事件定义的js函数在vue的mounted中挂载给window,执行禁止另存、打印、页面设置和打印预览的js

<template>
<div class="Word">
  <div style="width:auto; height:700px;" v-html="poHtmlCode" >
  </div>
</div>
</template>
 
<script>
  const axios=require('axios');
  export default{
    name: 'Word',
    data(){
      return {
        message: ' ',
        poHtmlCode: '',
 
      }
    },
    created: function(){
      //由于vue中的axios拦截器给请求加token都得是ajax请求,所以这里必须是axios方式去请求后台打开文件的controller
      axios.post("/api/ReadOnly/Word").then((response) => {
        this.poHtmlCode = response.data;
 
      }).catch(function (err) {
        console.log(err)
      })
    },
    methods:{
      //控件中的一些常用方法都在这里调用,比如保存,打印等等
      AfterDocumentOpened() {
		  document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(4, false); //禁止另存
		  document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(5, false); //禁止打印
		  document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(6, false); //禁止页面设置
		  document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(8, false); //禁止打印预览
      }
    },
    mounted: function(){
      // 将vue中的方法赋值给window
	  window.AfterDocumentOpened = this.AfterDocumentOpened;
    }
}
</script>

三、后端

禁止选中和禁止右键同时设置,实现禁止复制的功能。OpenModeType.docReadOnly模式打开文件限制文件编辑。并且隐藏菜单栏、office工具栏和pageoffice自定义工具栏。前端和后端配合实现文件在线安全预览,防止文件内容泄露

@RestController
@RequestMapping(value = "/ReadOnly")
public class ReadOnlyController {
 
    @RequestMapping(value = "/Word")
    public String showWord(HttpServletRequest request) {
		WordDocument wordDoc=new WordDocument();
        wordDoc.setDisableWindowSelection(true);//禁止选中
        wordDoc.setDisableWindowRightClick(true);//禁止右键
 
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage("/api/poserver.zz");//设置服务页面
        poCtrl.setMenubar(false);//隐藏菜单栏
        poCtrl.setOfficeToolbars(false);//隐藏Office工具条
        poCtrl.setCustomToolbar(false);//隐藏自定义工具栏
        poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened");
        //设置页面的显示标题
        poCtrl.setCaption("演示:文件在线安全浏览");
        //打开Word文档
	    poCtrl.setWriter(wordDoc);//此句必须
        poCtrl.webOpen(D:\\doc\\ReadOnly\\test.doc", OpenModeType.docReadOnly, "张三");
        return poCtrl.getHtmlCode("PageOfficeCtrl1");
    }
}

四、效果

image

转载 https://blog.csdn.net/qq_44306545/article/details/128574459