打开密码保护的PDF文件,iTextSharp的 [英] Opening password-protected pdf file with iTextSharp

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

问题描述

我在做应该与密码显示PDF文件的应用程序。这是我的code:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!Page.IsPostBack)
    {
        尝试
        {
            字符串文件路径=的Request.QueryString [文件路径];
            如果(filePath.ToUpper()的endsWith(PDF))
            {
                copyPDF(文件路径);
            }
        }
        抓住
        {
            字符串消息=< SCRIPT LANGUAGE =JavaScript'等>警报(找不到文件通话记录司进行!')< / SCRIPT>中;
            ScriptManager.RegisterStartupScript(页,this.GetType(),消息,消息,假的);
        }
    }
}
公共无效copyPDF(字符串文件路径)
{
    iTextSharp.text.pdf.RandomAccessFileOrArray RA =新iTextSharp.text.pdf.RandomAccessFileOrArray(使用Server.Mappath(RESOLVEURL(文件路径)));
    如果(RA!= NULL)
    {
        System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream();
        字节[] PASSWORD = System.Text.ASCIIEncoding.ASCII.GetBytes(Secretinfo);
        iTextSharp.text.pdf.PdfReader thepdfReader =新iTextSharp.text.pdf.PdfReader(RA,密码);
        诠释页= thepdfReader.NumberOfPages;
        iTextSharp.text.Document pdfDoc =新iTextSharp.text.Document();
        iTextSharp.text.pdf.PdfCopy pdfCopy =新iTextSharp.text.pdf.PdfCopy(pdfDoc,MS);        pdfDoc.Open();
        INT I = 0;
        而(I<页)
        {
            pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader,I + 1));
            我+ = 1;
        }
        pdfDoc.Close();
        字节] byteInfo = ms.ToArray();
        Response.Clear();
        Response.ContentType =应用程序/ PDF
        Response.AddHeader(内容长度,byteInfo.Length.ToString());
        Response.BinaryWrite(byteInfo);
        Response.Flush();
        到Response.End();
    }
}

我的code没有问题打开PDF文件,没有密码,但它不能用密码打开的PDF,即使密码被提供。在应用程序执行捕捞来代替。似乎是什么毛病我code?

修改
我删除的捕捉的看到抛出的异常。


  

异常详细信息信息:System.ArgumentException:PdfReader不是所有者密码打开


报告说,错误的来源是51号线。

  49号线:在(I<页)
第50行:{
51号线:pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader,I + 1));
52号线:我+ = 1;
53号线:}


解决方案

有关加密的文档的iText(夏普)某些操作要求文档不仅打开与用户密码,而是和所有者密码。这对应于在PDF说明书中这些密码的定义:


  

是否需要额外的操作应在解密的文档被允许取决于哪个密码(如果有的话),该文件被打开,当供给上创建文档时所指定的任何访问限制:


  
  

      
  • 打开文档与正确的所有者的密码应该让全(所有者)访问该文件。这无限制地访问包括更改文档的密码和​​访问权限的能力。

  •   
  • 打开文档与正确的用户的密码(或使用默认密码打开一个文档),应允许根据文档的加密字典中指定的用户访问权限进行额外的操作。

  •   

  
  

(第7.6.3.1在 ISO 32000-1


的iText(夏普)目前并没有详细检查的在文档的加密字典中指定的用户访问权限的而是总是需要为需要一定的权限,然后复制一个文件整个页面的操作所有者密码明确就是其中之一。

这已经说了,在iText的(夏普)的开发人员都非常清楚(因为问了很多这样的问题)


  • 这iText的(夏普)的用户可能有权即使没有所有者密码的帐户执行这些操作之前提到的在文档的加密字典中指定的用户访问权限,

  • 有到其各自所有者(被别人prevent滥用)应用的所有者密码,然后忘记了无数PDF文件(或使用随机生成的一个从来不知道它开始)和

  • 这iText的(夏普)(开源的),可以很容易地被任何人不打补丁,尊重用户和所有者密码之间的差异。

要允许用户做什么,他们有权和prevent库的修补拷贝在S preading,iText的(夏普)包含一个覆盖该测试在 PdfReader 类:

  / **
 *本iText的开发商,如果你决定改变概不负责
 *此静态参数的值。
 * @since 5.0.2
 * /
公共静态布尔unethicalreading = FALSE;

因此​​,通过设置

  PdfReader.unethicalreading = TRUE;

在全球范围内覆盖此权限检查机制。

请尊重PDF作者的权利,只有当你确实有资格有问题,执行操作使用此覆盖。

I'm making an application that should display PDFs with password. This is my code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        try
        {
            string filePath = Request.QueryString["filePath"];
            if (filePath.ToUpper().EndsWith("PDF"))
            {
                copyPDF(filePath);
            }
        }
        catch
        {
            string message = "<script language='Javascript'>alert('File Not Found! Call Records Department for verification. ')</script>";
            ScriptManager.RegisterStartupScript(Page, this.GetType(), message, message, false);
        }
    }
}
public void copyPDF(string filePath)
{
    iTextSharp.text.pdf.RandomAccessFileOrArray ra = new iTextSharp.text.pdf.RandomAccessFileOrArray(Server.MapPath(ResolveUrl(filePath)));
    if (ra != null)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("Secretinfo");
        iTextSharp.text.pdf.PdfReader thepdfReader = new iTextSharp.text.pdf.PdfReader(ra, password);
        int pages = thepdfReader.NumberOfPages;
        iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document();
        iTextSharp.text.pdf.PdfCopy pdfCopy = new iTextSharp.text.pdf.PdfCopy(pdfDoc, ms);

        pdfDoc.Open();
        int i = 0;
        while (i < pages)
        {
            pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader, i + 1));
            i += 1;
        }
        pdfDoc.Close();
        Byte[] byteInfo = ms.ToArray();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", byteInfo.Length.ToString());
        Response.BinaryWrite(byteInfo);
        Response.Flush();
        Response.End();
    }
}

My code has no problem opening pdf files without password but it can't open pdfs with password even though the password is supplied. The application executes the catch instead. What seems to be wrong with my code?

EDIT: I removed the Catch to see the exception thrown.

Exception Details: System.ArgumentException: PdfReader not opened with owner password

It says the source of the error is Line 51.

Line 49:    while (i < pages)
Line 50:    {
Line 51:         pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader, i + 1));
Line 52:         i += 1;
Line 53:    }

解决方案

For certain operations on encrypted documents iText(Sharp) requires that the document not merely is opened with the user password but instead with the owner password. This corresponds to the definition of these passwords in the PDF specification:

Whether additional operations shall be allowed on a decrypted document depends on which password (if any) was supplied when the document was opened and on any access restrictions that were specified when the document was created:

  • Opening the document with the correct owner password should allow full (owner) access to the document. This unlimited access includes the ability to change the document’s passwords and access permissions.
  • Opening the document with the correct user password (or opening a document with the default password) should allow additional operations to be performed according to the user access permissions specified in the document’s encryption dictionary.

(section 7.6.3.1 in ISO 32000-1)

iText(Sharp) currently does not check in detail the user access permissions specified in the document’s encryption dictionary but instead always requires the owner password for operations requiring certain permissions, and copying whole pages from a document definitively is one of them.

This been said, the iText(Sharp) developers are very much aware (due to many such questions asked)

  • that iText(Sharp) users may be entitled to execute such operations even without the owner password on account of the before mentioned user access permissions specified in the document’s encryption dictionary,
  • that there are myriad PDFs to which their respective owners applied an owner password (to prevent misuse by others) and then forgot it (or by using a randomly generated one never knew it to start with), and
  • that iText(Sharp) (being open source) can easily be patched by anyone not to respect the differences between user and owner password.

To allow users to do what they are entitled to and to prevent the spreading of patched copies of the library, iText(Sharp) contains an override for this test in the PdfReader class:

/**
 * The iText developers are not responsible if you decide to change the
 * value of this static parameter.
 * @since 5.0.2
 */
public static bool unethicalreading = false;

Thus, by setting

PdfReader.unethicalreading = true;

you globally override this permission checking mechanism.

Please respect the rights of PDF authors and only use this override if you indeed are entitled to execute the operations in question.

这篇关于打开密码保护的PDF文件,iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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