如何将SAP Document Builder的word控件设置成只读模式
Recently I am working on a customer project and one customer requirement is they do not want to the word document be editable in the word control. That means all buttons and menus in toolbar should be disabled.
The first idea comes to my mind is the flag “enableReadWrite“.
As documented in sap help,it can fulfill my help but unfortunately it is deprecated. Regardless of this warning I have a try and found it does not work indeed.
Then I am speculating that if the document uploaded is read only, it is for sure that the toolbar will be disabled. So the issue turns to that how could I mark the document to be read only on the fly during uploading.
Since word 2007 the format of MS office follows the so called “Open office” protocol whose specification could be found here. If you change the file type extension from .docx to .zip and open it with WinRAR, you will find the document is actually a bundle of several single files ( called document part in SAP internal ). The editability is controlled within the file settings.xml.
If you don’t know the exact syntax, just google it. I use the explanation from this one in google:
Now the task is quite easy, just add the necessary xml tag into the document source code. You do not need to manually parse the document source code since SAP has already done this job. You can just reuse standard class CL_DOCX_DOCUMENT. Since I need to insert the document protection node in “settings” node, a simple transformation is written for this. The magic is between line 18 and 21.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14" version="1.0">
<xsl:output encoding="UTF-8" indent="no" method="xml" omit-xml-declaration="no" version="1.0"/>
<!-- Match everything all nodes and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="w:settings">
<xsl:element name="w:settings">
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:element name="w:documentProtection">
<xsl:attribute name="w:edit">readOnly</xsl:attribute>
<xsl:attribute name="w:enforcement">1</xsl:attribute>
</xsl:element>
<xsl:copy-of select="./*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
and find a proper place to call the transformation:
DATA: lr_element TYPE REF TO if_wd_context_element,
lv_file_data TYPE xstring,
lv_ret TYPE i,
lx_temp TYPE xstring,
lv_msg TYPE string,
lt_parms TYPE /ipro/tt_key_value_pair,
ls_parm LIKE LINE OF lt_parms.
lr_element = me->wd_context->get_element( ).
CHECK lr_element IS NOT INITIAL.
lr_element->get_attribute( EXPORTING name = 'BINARY' IMPORTING value = lv_file_data ).
DATA(lo_docx) = cl_docx_document=>load_document( lv_file_data ).
DATA(lo_main_part) = lo_docx->get_maindocumentpart( ).
DATA(lo_docx_settings) = lo_main_part->get_documentsettingspart( ).
DATA(lx_settings) = lo_docx_settings->get_data( ).
/ipro/cl_docx_utilities=>transform( EXPORTING iv_input_xstring = lx_settings
iv_transform_name = '/IPRO/DOCXCC_PROTECT'
it_parameters = lt_parms
IMPORTING ev_result = lx_temp
ev_ret = lv_ret
ev_message = lv_msg ).
lo_docx_settings->feed_data( lx_temp ).
DATA(lx_docx_package) = lo_docx->get_package_data( ).
lr_element->set_attribute( EXPORTING name = 'BINARY' value = lx_docx_package ).
after that the tag will be there in settings.xml:
The word control before upload document looks like below, buttons and menus available:
After upload, menu and button are disabled. If you try to edit the document, there will be notifications in the right pane to give you a hint that is not possible.
Of course this solution does not work for lower version of MS word like word2003. Fortunately my customer has enough money and they are already using Office 2013 so I do not need to worry about it.
- bootstrap源码分析之scrollspy(滚动侦听)
- volatile
- 用最简单的图文带你看懂区块链
- 微信小游戏正式上线!小程序的一个类目,即点即玩!
- CentOS7安装GitLab、汉化、邮箱配置及使用
- Bootstrap源码分析之transition、affix
- Redis从单机到集群,一步步教你环境部署以及使用
- C++内存管理学习堆和栈
- 人人公司宣布收购美国卡车社区平台Trucker Path
- Mysql字符串截取总结:left()、right()、substring()、substring_index()
- c++程序的内存格局
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定
- 门罗盘点:2017年那些AI击败人类的瞬间
- sizeof和strlen的区别
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Mysql 必知必会(一)
- Spring boot 整合dynamic实现多数据源
- Integer 值判断相等
- JDK 8 新特性 之 Strams简单使用
- BindingException: Parameter 'XXX' not found. Available parameters are [collection, list]
- Android StartService()源码分析(一)
- JDK 8 新特性 之 default关键字
- 设计模式 之 单例模式
- SpringBoot 配置多数据源
- git pull 报错:The following untracked working tree files would be overwritten by merge
- Docker 使用Dockerfile构建Docker(三)
- Docker-compose 安装与基本使用(四)
- Docker-compose 常用命令及网络设置(五)
- HashMap 源码分析
- Java 按位运算符(&,|,^,>>,<<,>>>)