Reading .pdf file to a char[] array

Posted on 2005-04-13
Medium Priority
Last Modified: 2010-04-16
Thanks in advance for any help.

I'm trying to figure out the best way to fill a char[] from a FileInfo object representing the path to a .pdf file.
I guess I'm just looking for a general outline for using streams to read in the .pdf file, and getting it into a
Ultimately, the end result is this call to load the report in a browser window:

Question by:ViceroyFizzlebottom
  • 2

Accepted Solution

mikkelp earned 750 total points
ID: 13781913
You should probably use Response.BinaryWrite(byte[]) in stead)..

I've pieced some example code together from an app I have running. No guarantee the code will work

Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition","attachment; filename=" + pdffilename);
// I think you can do: Response.AppendHeader("content-disposition","inline; filename=" + pdffilename); too
Response.Charset = "";

// get fileinfo, and open a stream-reader
FileInfo fi = new FileInfo("pdffilename.pdf");
StreamReader sr = new StreamReader(fi.OpenRead());

//declare a buffer to store content of file in
byte[] buffer = new byte[1024];
int recv = 0;

// while you can continue reading 1kb from stream
// output entire buffer
while((recv = sr.Read(buffer, 0, 1024)) == 1024)

// If the last part was 1-1023 bytes
// get that in a smaller buffer, and output it.
        byte[] spare = new byte[recv];
        Array.Copy(buffer, 0, spare, 0, recv);


btw. there is some funny business with pdf-attachments when read inline in IE, because of the way the pdf-plugin (and other plugins) are handled.. It reads the page twice (first pass determines that IE should use the pdf-plugin, second pass reads the content of the file). The problem is that the pdf is pushed to the client twice, which results in a malformed pdf... I read about some fixes (well, hacks really) but haven't found a decent way of avoiding this - solution I opted for was "attachment" in stead of "inline"


Author Comment

ID: 13783326
This line gives errors of unable to convert from byte[] to char[]
while((recv = sr.Read(buffer, 0, 1024)) == 1024)

Any suggestions?

Expert Comment

ID: 13788654
doh, my bad (as mentioned, I haven't tested the code)

//  get a filestream
FileStream fs = fi.OpenRead();
while((recv = fs.Read(buffer, 0, 1024) == 1024)


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question