I want to check if a pdf file is password protected or not to view. That is i want to know if the pdf file has user password or not.
I found some help in some forum
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"); }
}