zoukankan      html  css  js  c++  java
  • 使用iText对pdf做权限的操作(不允许修改,不允许复制,不允许另存为),并且加水印等

    添加水印,并且增加权限

    @Test
        public void addWaterMark() throws Exception{
            String srcFile="D:\work\pdf\win10.pdf";//要添加水印的文件  
            String text="系统集成公司";//要添加水印的内容
              int textWidth=200;
              int textHeight=440;
              PdfReader reader = new PdfReader(srcFile);// 待加水印的文件
              PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File("D:\work\pdf\addWaterMark.pdf")));// 加完水印的文件
    //          byte[] userPassword = "123".getBytes();
              byte[] ownerPassword = "12345".getBytes();
    //          int permissions = PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_MODIFY_CONTENTS|PdfWriter.ALLOW_PRINTING;
    //          stamper.setEncryption(null, ownerPassword, permissions,false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_ASSEMBLY, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_COPY, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_DEGRADED_PRINTING, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_FILL_IN, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_CONTENTS, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_SCREENREADERS, false);
              stamper.setEncryption(null, ownerPassword, PdfWriter.DO_NOT_ENCRYPT_METADATA, false);
              stamper.  setViewerPreferences(PdfWriter.HideToolbar|PdfWriter.HideMenubar);
    //          stamper.setViewerPreferences(PdfWriter.HideWindowUI);
              int total = reader.getNumberOfPages() + 1;
              PdfContentByte content;
              BaseFont font = BaseFont.createFont("font/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
              for (int i = 1; i < total; i++)// 循环对每页插入水印
              {
                content = stamper.getUnderContent(i);// 水印的起始
                content.beginText();// 开始
                content.setColorFill(BaseColor.GREEN);// 设置颜色 默认为蓝色
                content.setFontAndSize(font, 38);// 设置字体及字号
                content.setTextMatrix(textWidth, textHeight);// 设置起始位置
                content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);// 开始写入水印
                content.endText();
                }
                stamper.close();
        }

    如果是在网页页面中的一部分显示可以使用js插件,在线PDF预览插件PDFObject.js

    如果为了兼容IE8可能就需要你想其他的办法

    我的解决办法是使用html自带的<object>标签,

    如果你只是让别人看还不能保存的话,可以将stamper.setViewerPreferences(PdfWriter.HideWindowUI);这句话的注释打开,在IE8也能使用

    我使用的是<object>在<DIV>的左移叠加隐藏(在火狐浏览器好像不能使用)

    <title>here</title>
    <style type="text/css">
        #showPdf{width:705px; height:400px;margin:10px 20px 30px 40px; overflow:hidden;toolBar:hidden;border:5px solid #000}
    </style>
    </head>
    <body onload="pdfObject.setShowToolbar(false);">
        <div id="showPdf" >
        <p style="margin-left:-45px;margin-bottom:10px;">
        <object name="pdfObject" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" type="application/pdf"  width="750px" height="400px" border="0" > 
                  <param   name="SRC"   value="<%=request.getContextPath()%>/waterMark.pdf">
        </object>
        </p>
        </div>
    <h1>生活不是我们活过的日子,而是我们记住的日子,为了讲述而在记忆中重现的日子。</h1>

    欢迎大家阅览,多多评论其中的不足!!

    为工程师之路添砖加瓦!!

  • 相关阅读:
    css3动画事件 animationend animationiteration animationstart
    dom对象---增加class属性,去除class属性
    数组的indexOf() 方法
    line-height中的五种取值方式和继承
    html 中line-height相关的四种box模型
    真正的能理解CSS中的line-height,height与line-height
    background-size:contain与cover的区别
    激活win10系统
    table-layout:fixed 应用
    js数组fill()方法
  • 原文地址:https://www.cnblogs.com/shinelover/p/5815132.html
Copyright © 2011-2022 走看看