使用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>

 

 

歡迎大家閱覽,多多評論其中的不足!!

為工程師之路添磚加瓦!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM