Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-To: Validate a FileStream is a valid PDF document with .NET [duplicate]

Possible Duplicate:
Detect if PDF file is correct (header PDF)

I want to validate that the data in a FileStream instance represents a valid PDF document. Specifically, I need to know that Adobe Reader will be able to successfully open the file.

Can anyone recommend an open source library or best practice for this task?

like image 839
smartcaveman Avatar asked Jan 11 '12 14:01

smartcaveman


1 Answers

Take a look at iTextSharp , it should give you what you need.

EDIT:

I know it's bad practice to use exceptions to control flow, but you could do this:

public bool IsValidPdf(string fileName)
{
   try
   {
      new iTextSharp.text.pdf.PdfReader(fileName);
      return true;
   }
   catch (iTextSharp.text.exceptions.InvalidPdfException)
   {
      return false;
   }
}
like image 165
Myles McDonnell Avatar answered Sep 18 '22 19:09

Myles McDonnell