How to Detect If Source PDF File is Password Protected or Not inside .NET Apps
Join the DZone community and get the full member experience.
Join For Free//C# Code Sample
// load the source PDF document
PdfFileInfo fileInfo = new PdfFileInfo(@"D:\PDF_OpenPassword.pdf");
// determine that source PDF file is Encrypted with password
bool encrypted = fileInfo.IsEncrypted;
// MessageBox displays the current status related to PDf encryption
MessageBox.Show(encrypted.ToString());
//VB.NET Code Sample
' load the source PDF document
Dim fileInfo As PdfFileInfo = New PdfFileInfo("D:\PDF_OpenPassword.pdf")
' determine that source PDF file is Encrypted with password
Dim encrypted As Boolean = fileInfo.IsEncrypted
' MessageBox displays the current status related to PDf encryption
MessageBox.Show(encrypted.ToString())
//Get information about PDF document security
PdfFileInfo contains three properties to get information about PDF document security.
//C# Code Sample
PdfFileInfo fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithNoOpenPasswordAndPermPassword.pdf"));
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.User, fileInfo.PasswordType);
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithNoOpenPasswordAndPermPassword.pdf"), "password");
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.Owner, fileInfo.PasswordType);
Assert.False(fileInfo.HasOpenPassword);
Assert.True(fileInfo.HasEditPassword);
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithOpenPasswordAndNoPermPassword.pdf"));
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.Inaccessible, fileInfo.PasswordType);
Assert.True(fileInfo.HasOpenPassword);
try
{
bool hasOwnerPassword = fileInfo.HasEditPassword;
Assert.Fail("When PasswordType is Inaccessible we can't read HasEditPassword property.");
}
catch (InvalidPasswordException e)
{
// right what we expect
}
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithOpenPasswordAndNoPermPassword.pdf"), "password");
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.User, fileInfo.PasswordType);
Assert.True(fileInfo.HasOpenPassword);
Assert.False(fileInfo.HasEditPassword);
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithOpenPasswordAndPermPassword.pdf"));
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.Inaccessible, fileInfo.PasswordType);
Assert.True(fileInfo.HasOpenPassword);
try
{
bool hasOwnerPassword = fileInfo.HasEditPassword;
Assert.Fail("When PasswordType is Inaccessible we can't read HasEditPassword property.");
}
catch (InvalidPasswordException e)
{
// right what we expect
}
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithOpenPasswordAndPermPassword.pdf"), "password1");
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.User, fileInfo.PasswordType);
Assert.True(fileInfo.HasOpenPassword);
Assert.True(fileInfo.HasEditPassword);
fileInfo = new PdfFileInfo(TestSettings.GetInputFile("PdfWithOpenPasswordAndPermPassword.pdf"), "password2");
Assert.True(fileInfo.IsEncrypted);
Assert.AreEqual(PasswordType.Owner, fileInfo.PasswordType);
Assert.True(fileInfo.HasOpenPassword);
Assert.True(fileInfo.HasEditPassword);
//Determine correct password from Array
//Sometimes there is a requirement to determine the correct password from array of passwords and open the document with correct password. The following code snippet demonstrates the steps to iterate through the array of passwords and try opening the document with correct password.
//C# Code Sample
// load source PDF file
PdfFileInfo info = new PdfFileInfo();
info.BindPdf("c:/pdftest/Lorem+Ipsum_protected.pdf");
// determine if the source PDF is encrypted
Console.WriteLine("File is password protected " + info.IsEncrypted);
String[] passwords = new String[5]{"test","test1","test2","test3","sample"};
for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
{
try
{
Document doc = new Document("c:/pdftest/Lorem+Ipsum_protected.pdf", passwords[passwordcount]);
if (doc.Pages.Count>0)
Console.WriteLine("Number of Page in document are = "+doc.Pages.Count);
}
catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
{
Console.WriteLine("Password = "+passwords[passwordcount]+ " is not correct");
}
}
//VB.NET Code Sample
' load source PDF file
Dim info As PdfFileInfo = New PdfFileInfo()
info.BindPdf("c:/pdftest/Lorem+Ipsum_protected.pdf")
' determine if the source PDF is encrypted
Console.WriteLine("File is password protected " & info.IsEncrypted)
Dim passwords() = New String() {"test", "test1", "test2", "test3", "sample"}
For passwordcount As Integer = 0 To passwords.Length - 1
Try
Dim doc As Document = New Document("c:/pdftest/Lorem+Ipsum_protected.pdf", passwords(passwordcount))
If (doc.Pages.Count > 0) Then
Console.WriteLine("Number of Page in document are = " & doc.Pages.Count)
End If
Catch ex As Aspose.Pdf.Exceptions.InvalidPasswordException
Console.WriteLine("Password = " & passwords(passwordcount) & " is not correct")
End Try
Next
End Sub
Opinions expressed by DZone contributors are their own.
Comments