Reading .pdf file to a char[] array

Posted on 2005-04-13
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
    LVL 4

    Accepted Solution

    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"

    LVL 6

    Author Comment

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

    Any suggestions?
    LVL 4

    Expert Comment

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now