13

我正在制作一个应该显示带有密码的 PDF 的应用程序。这是我的代码:

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。应用程序改为执行 catch。我的代码似乎有什么问题?

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

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

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

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

3 回答 3

20

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

是否允许对解密的文档进行其他操作取决于打开文档时提供的密码(如果有)以及创建文档时指定的任何访问限制:

  • 使用正确的所有者密码打开文档应该允许完全(所有者)访问文档。这种无限制的访问包括更改文档密码和访问权限的能力。
  • 使用正确的用户密码打开文档(或使用默认密码打开文档)应该允许根据文档加密字典中指定的用户访问权限执行附加操作。

( ISO 32000-1中的第 7.6.3.1 节)

iText(Sharp) 目前不会详细检查文档加密字典中指定的用户访问权限,而是始终需要所有者密码才能进行需要某些权限的操作,而从文档中复制整个页面就是其中之一。

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

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

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

/**
 * 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;

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

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

于 2013-07-17T08:35:24.680 回答
1

尝试使用此解决方法来解锁受保护的 PdfReader。这对我有用:

public static PdfReader TryToUnlockPdf(PdfReader reader)
{
    if (reader == null)
    {
        return reader;
    }
    try
    {
        var f = reader.GetType().GetField("encrypted", BindingFlags.NonPublic | BindingFlags.Instance);
        f?.SetValue(reader, false);
    }
    catch (Exception)
    { // ignore
    }
    return reader;
}

private static void GetPdfPageFiles(this Page pageFile)
{
    reader = new PdfReader(pageFile.ContentBytes);
    // Unlock protected 
    reader = TryToUnlockPdf(reader);

    // if no using TryToUnlockPdf workaroud - GetImportedPage method raises "System.ArgumentException: PdfReader not opened with owner password"
    var curPage = pdfWriter.GetImportedPage(reader, 0);
}
于 2020-05-21T08:40:07.003 回答
1

我应用了这个解决方法并且它有效:

private void fixIssue(PdfReader pdfReader) throws Exception {
        Field f = pdfReader.getClass().getDeclaredField("ownerPasswordUsed");
        f.setAccessible(true);
        f.setBoolean(pdfReader, true);

}
于 2015-12-07T13:02:46.213 回答