使用iTextSharp加密PDF文档 [英] Encrypt PDF document using iTextSharp

查看:405
本文介绍了使用iTextSharp加密PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过禁止填写和复制文档来保护自己的PDF文档.我为此使用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();

生成文档时,我使用该代码对文档进行加密.但是稍后,当我在Adobe Reader中打开文档(在9和11上测试)并检查文件>属性>安全性"时,它们对文档的填写和复制没有任何限制,并且其状态为允许".

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?

推荐答案

根据 PdfStamper 的ITextSharp文档,此方法的第二个参数是代表加密的PDF文档数据目标的输出流.您在问题中显示的代码在设置加密后仅会处理MemoryStream,因此该代码可能会应用于您的PDF文档的任何更改都不会保存到磁盘上,否则将无法在您的应用程序外部使用.

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.

这篇关于使用iTextSharp加密PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆