Hi,
I would like to check whether the given PDF file is valid PDF file and whether it has file open password or not. Here are the sample file I am using:
FileOpenPasswordProtected.pdf (85.9 KB)
InvalidFileType.pdf (17.5 KB)
The code I have so far:
static void Main()
{
SetLicense();
//var inFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileOpenPasswordProtected.pdf");
var inFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InvalidFileType.pdf");
CheckPdf(inFile);
}
private static void CheckPdf(string inFile)
{
PdfFileInfo pdfFileInfo = new PdfFileInfo(inFile);
Console.WriteLine("Is Password protected: " + pdfFileInfo.HasOpenPassword);
Console.WriteLine("Is Valid PDF file: " + pdfFileInfo.IsPdfFile);
}
The problem I have is, for both scenarios (file open and invalid pdf), IsPdfFile is false. HasOpenPassword is not initialized when PDF file is not a valid file.So, I am getting NullReferenceException exception when I try validating the InvalidFileType.pdf.
So, I need to know how I can identify whether the given file is invalid without getting any exceptions in the above code.
Thanks