Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:07+00:00 2026-05-16T22:16:07+00:00

Given a stream of bytes, how can I tell if this stream contains a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:16:07+00:00Added an answer on May 16, 2026 at 10:16 pm

    It all depends of how well/reliable you want the detection working.

    Here my selection of most important bits+pieces from the 756 page long official definition, straight from the horse’s mouth (PDF 32000:1-2008):

    A basic conforming PDF file shall be constructed of following four elements (see Figure 2):

    • A one-line header identifying the version of the PDF specification to which the file conforms
    • A body containing the objects that make up the document contained in the file
    • A cross-reference table containing information about the indirect objects in the file
    • A trailer giving the location of the cross-reference table and of certain special objects within the body of the file
      [….]

    The first line of a PDF file shall be a header consisting of the 5 characters %PDF– followed by a version number of the form 1.N, where N is a digit between 0 and 7.
    A conforming reader shall accept files with any of the following headers:
    %PDF–1.0
    %PDF–1.1
    %PDF–1.2
    %PDF–1.3
    %PDF–1.4
    %PDF–1.5
    %PDF–1.6
    %PDF–1.7
    […]

    If a PDF file contains binary data, as most do (see 7.2, “Lexical Conventions”), the header line shall be immediately followed by a comment line containing at least four binary characters—that is, characters whose codes are 128 or greater. This ensures proper behaviour of file transfer applications that inspect data near the beginning of a file to determine whether to treat the file’s contents as text or as binary.

    Trailer
    [….] The last line of the file shall contain only the end-of-file marker, %%EOF. The two preceding lines shall contain, one per line and in order, the keyword startxref and the byte offset in the decoded stream from the beginning of the file to the beginning of the xref keyword in the last cross-reference section.

    Summary

    Two of the most important thing to remember:

    (a) The first ‘header line’

       %PDF-1.X
    

    [where X in 0..7] must appear on a line of its own be followed by a newline. This line must appear within the first 4096 Bytes, not necessarily on the very first line. The preceding lines may contain non-PDF content, but printer job languange commands (PJL) or comments.

    (b) The very next line must be four binary bytes if the PDF contains binary data.

    Just parsing for ‘%PDF-1.’ has bitten a lot of people already….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've posted this question in asp.net web forum but no one replied. I can't
Given a stream of JSON, how can I read it as XML. I am
Is there a way to stream a file using ASP.NET MVC FileContentResult within the
Given that Groovy does not have a do-while statement, how can I iterate over
I have a stream of bytes (given by a BufferedInputStream) which represents a sequence
Given two stream-oriented I/O objects in Asio , what is the simplest way to
Given a data stream of continuously arriving items containing a timestamp and text (e.g.
Given the line: Bitmap bitmap = new Bitmap(stream); where stream is a System.IO.Stream, are
I'm reading data from a stream into a char array of a given length,
Given long long int x, y; , I want a function that can compare

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.