AUTHOR: Stefan Stefanov
DATE MODIFIED : May 26, 2017
DATE POSTED: May 07, 2015
The reason for the observed behavior is an invalid pdf file according to the pdf specification. Such files contain empty bytes after the %%EOF marker.
By specification within the last 1024 bytes of the pdf file the %%EOF marker should be found and when we attempt to look for it, we can’t locate it because of these empty bytes, which should not be in the document at first place. Here is a quote from the adobe pdf specs:
3.4.4, “File Trailer”
18.Acrobat viewers require only that the %%EOF marker appear somewhere within the last 1024 bytes of the file.
The solution is to trim those empty bytes and continue with the document loading.
 bytes = File.ReadAllBytes(@
eofPos = -1;
i = bytes.Length - 1; i >= 0; i--)
(bytes[i] != 0)
eofPos = i;
 newBytes =
[eofPos + 1];
Array.Copy(bytes, newBytes, eofPos + 1);
MemoryStream ms =