简体   繁体   中英

Encrypt PDF document using iTextSharp

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM