【问题标题】:checking if pdf is password protected using itextsharp使用 itextsharp 检查 pdf 是否受密码保护
【发布时间】:2023-03-26 11:25:02
【问题描述】:

我想检查一个 pdf 文件是否有密码保护或不查看。那就是我想知道pdf文件是否有用户密码。

我在一些论坛上找到了一些关于它使用isencrypted 功能的帮助,但它没有给出正确的答案。

是否可以检查 pdf 是否受密码保护?

【问题讨论】:

  • I found some help in some forum about it to use isencrypted function but it does not give correct answer -- 这听起来并不令人鼓舞。

标签: c# .net pdf itextsharp


【解决方案1】:

使用PdfReader.IsEncrypted 方法的问题在于,如果您尝试在需要密码的PDF 上实例化PdfReader,并且您没有提供该密码,您将获得BadPasswordException。

记住这一点,您可以编写如下方法:

public static bool IsPasswordProtected(string pdfFullname) {
    try {
        PdfReader pdfReader = new PdfReader(pdfFullname);
        return false;
    } catch (BadPasswordException) {
        return true;
    }
}

请注意,如果您提供的密码无效,则在尝试构造 PdfReader 对象时会得到相同的 BadPasswordException。您可以使用它来创建验证 PDF 密码的方法:

public static bool IsPasswordValid(string pdfFullname, byte[] password) {
    try {
        PdfReader pdfReader = new PdfReader(pdfFullname, password);
        return false;
    } catch (BadPasswordException) {
        return true;
    }
}

当然它很丑,但据我所知,这是检查 PDF 是否受密码保护的唯一方法。希望有人能提出更好的解决方案。

【讨论】:

  • 不错的方法,对我有用,我只是对您的代码进行了一些更改,您在 IsPasswordValid 上有真假返回,导致密码有效时的非直观答案,它返回错误,反之亦然反之亦然!
  • 您应该更新示例以包含对 PdfReader 对象的 Dispose() 调用。
  • 似乎在打开时不再抛出错误,等待您采取行动。有一个完全访问属性可以检查以确定是否存在未提供的密码
【解决方案2】:
  private void CheckPdfProtection(string filePath)
        {
            try
            {
                PdfReader reader = new PdfReader(filePath);
                if (!reader.IsEncrypted()) return;
                if (!PdfEncryptor.IsPrintingAllowed(reader.Permissions))
                    throw new InvalidOperationException("the selected file is print protected and cannot be imported");
                if (!PdfEncryptor.IsModifyContentsAllowed(reader.Permissions))
                    throw new InvalidOperationException("the selected file is write protected and cannot be imported");
            }
            catch (BadPasswordException) { throw new InvalidOperationException("the selected file is password protected and cannot be imported"); }
            catch (BadPdfFormatException) { throw new InvalidDataException("the selected file is having invalid format and cannot be imported"); }
        }

【讨论】:

    【解决方案3】:

    参考:Check for Full Permission

    您应该能够只检查属性 PdfReader.IsOpenedWithFullPermissions。

    PdfReader r = new PdfReader("YourFile.pdf");
    if (r.IsOpenedWithFullPermissions)
    {
        //Do something
    }
    

    【讨论】:

    • 没有。它在这一行给出了例外 PdfReader r = new PdfReader("YourFile.pdf");用于受密码保护的文件。只需使用此代码检查受密码保护的 pdf 文件。
    • 用密码保护的文件检查这个......你可以看到异常 try { PdfReader r = new PdfReader("YourFile.pdf"); if (r.IsOpenedWithFullPermissions) { //做点什么 } } catch(Exception ex) { MessageBox.Show(ex.ToString()); }
    • @MdKamruzzamanPallob:您能在您的问题中添加例外吗? (对其他人进行设置并非易事,因此他们不太可能这样做 - 在寻求帮助时,请详细说明您的答案并提供帮助!)
    【解决方案4】:

    以防万一它最终帮助某人,这是我在 vb.net 中使用的一个简单解决方案。使用完全权限检查的问题(如上所述)是您实际上无法打开具有阻止您打开它的密码的 PDF。在下面的代码中,我也有一些关于检查的事情。 itextsharp.text.pdf 有一些您可能会发现实际上有用的例外情况,如果这没有满足您的需要,请检查一下。

    Dim PDFDoc As PdfReader
    Try
        PDFDoc = New PdfReader(PDFToCheck)
    If PDFDoc.IsOpenedWithFullPermissions = False Then
       'PDF prevents things but it can still be opened. e.g. printing.
    end if
    Catch ex As iTextSharp.text.pdf.BadPasswordException
        'this exception means the PDF can't be opened at all. 
    Finally 
        'do whatever if things are normal!
    End Try
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2019-07-01
      • 2013-07-14
      相关资源
      最近更新 更多