Alex Rivera | Logout

How do I know if a File type is PDF?

Asked 2012-11-08T20:09:14.910
9
  • This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not

  • Using complete library for this use looks like overkill

  • Are there any ways to know that a Java File is of type PDF?
Edit
Report

2 Answers

19

Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.

answered 2012-11-08T20:13:32.480
2

If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file

PDF files start with "%PDF" (hex 25 50 44 46).
answered 2012-11-08T20:14:03.310

Your Answer