How-To: FileStream File Validation is a valid PDF document with .NET.

Possible duplicate:
Finding the correct PDF file (PDF header)

I want to verify that the data in the FileStream instance represents a valid PDF document. In particular, I need to know that Adobe Reader can successfully open a file.

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

+4
source share
2 answers

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

EDIT:

I know that it is wrong to use exceptions to control the flow, but you can do this:

 public bool IsValidPdf(string fileName) { try { new iTextSharp.text.pdf.PdfReader(fileName); return true; } catch (iTextSharp.text.exceptions.InvalidPdfException) { return false; } } 
+11
source

Open the file in memory and check if there is a first line:

 %PDF-1.5 

or

 %PDF-1.4 

or a simple% pdf line.

it is not very safe, but it worked for me.

0
source

Source: https://habr.com/ru/post/1390441/


All Articles