C# 使用itextsharp检查pdf是否受密码保护

C# 使用itextsharp检查pdf是否受密码保护,c#,.net,pdf,itextsharp,C#,.net,Pdf,Itextsharp,我想检查pdf文件是否受密码保护或无法查看。也就是说,我想知道pdf文件是否有用户密码 我在一些论坛上找到了一些关于使用isencrypted函数的帮助,但它没有给出正确的答案 是否可以检查pdf是否受密码保护?参考: 您应该能够只检查属性PdfReader.IsOpenedWithFullPermissions PdfReader r = new PdfReader("YourFile.pdf"); if (r.IsOpenedWithFullPermissions) { //Do s

我想检查pdf文件是否受密码保护或无法查看。也就是说,我想知道pdf文件是否有用户密码

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

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

参考:

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

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

使用
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是否受密码保护的唯一方法。希望有人能提出更好的解决方案。

为了以防万一,我在vb.net中使用了一个简单的解决方案。使用fullpermissions(如上所述)进行检查的问题是,如果PDF的密码阻止您打开,您实际上无法打开该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

我在一些论坛上找到了一些关于使用iEncrypted函数的帮助,但它没有给出正确的答案
——这听起来并不令人鼓舞。不。它在这一行中给出了异常PdfReader r r=newpdfreader(“YourFile.pdf”);用于密码保护的文件。只需使用此代码检查受密码保护的pdf文件。使用受密码保护的文件检查此。。。。您可以看到异常try{PdfReader r=new PdfReader(“YourFile.pdf”);if(r.IsOpenedWithFullPermissions){//Do something}}catch(异常ex){MessageBox.Show(ex.ToString());}@MdKamruzzamanPallob:您能将异常添加到您的问题中吗?(这对其他人来说是非常重要的,所以他们不太可能这么做-在寻求帮助时,请详细说明并帮助您的答案!)很好的方法,对我很有用,我只是对您的代码做了一些更改,您在IsPasswordValid上的返回值是真是假,导致密码有效时的回答不直观,它返回错误,反之亦然!您应该更新该示例以包含对PdfReader对象的Dispose()的调用。该错误似乎不再在open上抛出,并等待您采取操作。有一个完全访问属性,可以检查该属性以确定是否存在未提供的密码
  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"); }
        }