Check whether a PDF is valid or not in C# using Aspose.PDF for .NET


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()

	//var inFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileOpenPasswordProtected.pdf");
	var inFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InvalidFileType.pdf");

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.



We have also noticed the similar issue in our environment using Aspose.PDF for .NET 20.1. We have logged it as PDFNET-47630 in our issue tracking system for the sake of correction. We will further investigate the reasons behind this and keep you posted with the status of its rectification. Please be patient and spare us little time.

We are sorry for the inconvenience.

Is there any update on this ticket?


We regret to share that earlier logged ticket is not yet resolved. Please note that the issue was logged under normal support model and it has low priority. It will be resolved on first come first serve basis. We will surely inform you as soon as we have some definite updates regarding its resolution. Please spare us some time.

We are sorry for the inconvenience.