tags:

views:

96

answers:

2

hello,

given a stream of bytes, how can i tell if this stream contains a pdf document or something else?

i am using .net and c# but it does not matter.

thanks konstantin

+5  A: 

A PDF file starts with an ASCII string %PDF-1.3 or something similar, depending on what PDF version it actually is.

Carl Norum
Remember, too, that if the file starts with `%PDF-1.3`, that's a good indication that it's a PDF file, but not a guarantee. The only way to know for sure would be to parse the entire file.
Jim Mischel
Oh, dear. This is a way to vague criterion... Also, akonsu wasn't talking about a **file**, but a **stream of bytes**. (I wonder why such a sloppy answer gets 5 upvotes within 30 minutes...)
pipitas
@pipitas, well since the OP accepted it, I presume it satisfies his criteria. Your answer is clearly more thorough, so hopefully he'll come back and read it, too.
Carl Norum
@Carl Norum: well, his criteria are too loose then. Let's hope he doesn't get bitten by relying on them.
pipitas
+10  A: 
pipitas
@pipitas: thanks for the thorough explanation!
akonsu