马上注册,免费下载文档,上传文档可提现,享用更多功能。
您需要 登录 才可以下载或查看,没有账号?免费注册
×
屏蔽功能按钮
新版 pdf.js 使用style设置隐藏无效,经研究可以通过 setAttribute 较好的解决。
viewer.html 的<head>最后,添加:
- <script>
- function onBodyLoad()
- {
- var appConfig = PDFViewerApplication.appConfig;
- appConfig.toolbar.viewBookmark.setAttribute('hidden', 'true');
- appConfig.secondaryToolbar.viewBookmarkButton.setAttribute('hidden', 'true');
- appConfig.toolbar.openFile.setAttribute('hidden', 'true');
- appConfig.secondaryToolbar.openFileButton.setAttribute('hidden', 'true');
- appConfig.toolbar.download.setAttribute('hidden', 'true');
- appConfig.secondaryToolbar.downloadButton.setAttribute('hidden', 'true');
- appConfig.toolbar.print.setAttribute('hidden', 'true');
- appConfig.secondaryToolbar.printButton.setAttribute('hidden', 'true');
- }
- </script>
复制代码
<body> 添加下列属性:onload="onBodyLoad()"
这样,打开、打印、下载按钮就消失了
解决了下载按钮之后,我发现鼠标焦点在iframe里时 使用ctrl+s也能下载文件。解决这个问题同样也是找到pdf/web/viewer.js,注释下载代码就ok了。(不同版本所在行数可能不同,根据行数找不到时请根据内容搜索)

这样用户就无法下载pdf文件了,我们的目的就达到了
禁用鼠标选择、鼠标右键等操作,修改viewer.jsp的body标签,添加属性改为如下内容:
- <body tabindex="1" class="loadingInProgress"
- tabindex="1" class="loadingInProgress"
- oncontextmenu="return false;" leftMargin="0" topMargin="0"
- oncopy="return false;" oncut="return false;"
- onselectstart="return false">
复制代码
添加全局水印
利用canvas添加全局水印,其实就是在viewer.js中遍历文件元素节点的同时,创建水印元素节点并插入到每一页的位置。原文参考
1.在viewer.js中找到如下内容,位置大概在11973行前后:
- if (this.textLayerMode !== _ui_utils.TextLayerMode.DISABLE && this.textLayerFactory) {
- var textLayerDiv = document.createElement('div');
- textLayerDiv.className = 'textLayer';
- textLayerDiv.style.width = canvasWrapper.style.width;
- textLayerDiv.style.height = canvasWrapper.style.height;
- //---这里就是要插入水印的位置---
- if (this.annotationLayer && this.annotationLayer.div) {
- div.insertBefore(textLayerDiv, this.annotationLayer.div);
- } else {
- div.appendChild(textLayerDiv);
- }
- textLayer = this.textLayerFactory.createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, this.textLayerMode === _ui_utils.TextLayerMode.ENABLE_ENHANCE);
- }
复制代码
- if (this.textLayerMode !== _ui_utils.TextLayerMode.DISABLE && this.textLayerFactory) {
- var textLayerDiv = document.createElement('div');
- textLayerDiv.className = 'textLayer';
- textLayerDiv.style.width = canvasWrapper.style.width;
- textLayerDiv.style.height = canvasWrapper.style.height;
- //---------------------水印开始---------------------
- var cover = document.createElement('div');
- cover.className = "cover";
- cover.innerText = "内容保密,请勿复制或下载"; //这里就是水印内容,如果要按照不同的文件显示不同的水印,可参考pdf文件路径的传值方式,在viewer.jsp中head部位接收后台传值并在这里使用
- if (this.annotationLayer) {
- // annotationLayer needs to stay on top
- div.insertBefore(textLayerDiv, this.annotationLayer.div);
- div.appendChild(cover);
- } else {
- div.appendChild(textLayerDiv);
- div.appendChild(cover);
- }
- var coverEle = document.getElementsByClassName('cover'),size = 0,
- nowWidth = +canvasWrapper.style.width.split("p")[0],
- //714为100%时,每页的宽度。对比当前的宽度可以计算出页面变化后字体的数值
- size = 50 * nowWidth / 714 + "px";
- for(var i=0, len=coverEle.length; i<len; i++){
- coverEle[i].style.fontSize = size;
- coverEle[i].style.width = canvasWrapper.style.width;
- coverEle[i].style.height = canvasWrapper.style.height / 10;
- }
- //---------------------水印结束---------------------
- if (this.annotationLayer && this.annotationLayer.div) {
- div.insertBefore(textLayerDiv, this.annotationLayer.div);
- } else {
- div.appendChild(textLayerDiv);
- }
- textLayer = this.textLayerFactory.createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, this.textLayerMode === _ui_utils.TextLayerMode.ENABLE_ENHANCE);
- }
复制代码
3.最后在viewer.css文件开始位置添加水印的css样式完成水印显示:
- /* 水印遮罩层 */
- .cover{
- z-index: 100;
- position: absolute;
- top: 41%;
- left: 1%;
- transform: rotate(330deg);
- text-align: center;
- font-size: 310%;
- padding-left: 30px;
- letter-spacing: 18px;
- color:rgba(162, 162, 162, 0.4);
- }
复制代码 |
|