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

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

问题描述

我正在制作一个应用程序,它应该显示带有密码的 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();
    }
}

我的代码在没有密码的情况下打开 pdf 文件没有问题,但即使提供了密码,也无法打开带密码的 pdf.应用程序改为执行捕获.我的代码似乎有什么问题?

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?

编辑:我删除了 Catch 以查看抛出的异常.

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

异常详细信息:System.ArgumentException:PdfReader 未使用所有者密码打开

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

它说错误的来源是第 51 行.

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:    }

推荐答案

对于加密文档的某些操作,iText(Sharp) 要求文档不仅使用用户密码打开,而且使用所有者密码打开.这对应于 PDF 规范中这些密码的定义:

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.

(ISO 32000-1)

iText(Sharp) 目前没有详细检查文档加密字典中指定的用户访问权限,而是总是需要所有者密码来进行需要某些权限的操作,以及从文档中复制整个页面绝对是其中之一.

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.

据说,iText(Sharp) 开发人员非常清楚(由于提出了许多此类问题)

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

  • 鉴于上述文档加密字典中指定的用户访问权限,即使没有所有者密码,iText(Sharp)用户也可能有权执行此类操作,
  • 有无数的 PDF 文件,它们各自的所有者应用了所有者密码(以防止他人误用),然后又忘记了它(或使用了一个从不知道它开始的随机生成的密码),并且
  • iText(Sharp)(开源)可以很容易地被任何人修补而不考虑用户和所有者密码之间的差异.

为了允许用户做他们有权做的事情并防止库的修补副本传播,iText(Sharp) 在 PdfReader 类中包含此测试的覆盖:

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;

因此,通过设置

PdfReader.unethicalreading = true;

您全局覆盖此权限检查机制.

you globally override this permission checking mechanism.

请尊重 PDF 作者的权利,并且仅当您确实有权执行相关操作时才使用此覆盖.

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

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