I have created an app that uses Aspose.PDF to decrypt a series of files. Here is a snippet of code:
foreach(var _file in _files){
try{
PdfFileInfo _info = new PdfFileInfo(_file);
var _isEncrypted = _info.IsEncrypted;
_info.Close();
if(_isEncrypted)
{
using(var secureDoc = new PdfFileSecurity())
{
secureDoc.BindPdf(_file);
secureDoc.DecryptFile(_masterPassword);
secureDoc.SetPrivilege(_privelege); //_privelege defined earlier
secureDoc.Save(_file);
secureDoc.Close();
}
}
else { Console.WriteLine("{0} not encrypted",_file); }
}
catch(Exception e)
{
Console.WriteLine("{0} - Fail: {1}",_file,e.Message);
}
}
The first time through, this code works fine. PDF’s in the directory that have no password protection already simply get a console line stating that they are not encrypted.
After running the code on all files in a given directory, I can open the files without being prompted for a password – so it would appear that the file has been successfully decrypted. However, if I run the code a second time on the same directory, I would expect to get the “not encrypted” message on all the files; however, what I get is the Fail message on all the files that were previously encrypted with a message stating “Invalid Password”.
This tells me that the PdfFileInfo.IsEncrypted is still returning true on all the files that were supposedly decrypted. This does not make sense to me.
Can someone explain what is going on? Thanks.