Encrypt PDF document using iTextSharp

佐手、 提交于 2020-01-26 03:23:06

问题


I want to make my PDF document protected by not allowing fill in and copy from it. I am using iTextSharp for this. I have following code:

  PdfReader reader = new PdfReader(document, System.Text.Encoding.UTF8.GetBytes(PASSWORD));
  using (MemoryStream ms = new MemoryStream())
  {
      using (PdfStamper stamper = new PdfStamper(reader, ms))
      {
           stamper.SetEncryption(
               null,
               Encoding.ASCII.GetBytes(PASSWORD),
               PdfWriter.ALLOW_PRINTING,
               PdfWriter.ENCRYPTION_AES_128);
      }
  }

  reader.Close();

When the document is generated I use that code to encrypt the document. But later when I open the document in Adobe Reader (tested on 9 and 11) and check the 'File > Properties > Security' their are no restrictions applied on fill in and copy of the document and their status is Allowed.

Is there any issue in that code?


回答1:


According to the ITextSharp documentation for PdfStamper, the second parameter to this method is an output stream representing the destination for the encrypted PDF document data. The code you show in the question simply disposes the MemoryStream after you setup the encryption so any changes this code could apply to your PDF document will never be saved to disk or otherwise be available outside your application.



来源:https://stackoverflow.com/questions/21213849/encrypt-pdf-document-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!