AUTHOR: Stefan Stefanov
DATE MODIFIED : December 06, 2016
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 =
Copyright © 2016, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.