Trouble with Response.OutputStream

Posted on 2012-08-21
Last Modified: 2012-12-02
I have a script that generates a bunch of PDFs on the fly.  It works fine so long as it's the only code and I put it in Page_Load() event.  However, I want to put it inside of a Button Click event now and I'm having problems.

What happens is I click the button, call a routine to create the PDFs as follows:

Response.ContentType = "application/pdf";
// a bunch of code that creates the PDFs.

When I put it in the button click it works fine, but at the end I have a blank screen.  What I need is my form back with the button and other form objects.  That's not happening because I have the  Response.End statement in there.  The problem is, if I remove it I get the following Adobe Reader error:

File does not begin with '%PDF-'.

Thanks for any help.
Question by:dtburdick
    LVL 11

    Accepted Solution

    Once the headers had been send to the client (browser) it's too late to change the page content type. Button click event occurs way after the page is loaded so the browser already knows that it's a page instead of pdf file.
    You need to do this on page_load and rely on IsPostBack value to load the page or write the pdf.
    Other solution would be to open pdf in a new window. Use Content-Disposition=inline to display it in the browser otherwise you'll be asked to save the pdf and the new window will remain blank.

    Assisted Solution

    Thanks, so much.  I got it working based on what you said, but I have no idea what I'm doing here and I don't want to crush my server by doing it wrong.  Can you please tell me if this is going to cause a huge memory leak.  I am closing the writer, but I don't know if there is something I'm supposed to do to destroy the MemoryStream.  I know .NET has some Garbage Collector that cleans that kind of thing up, but I don't know how much I'm supposed to do.

    What I did was comment out all of the lines of code regarding the Response object and just ran the code the creates the PDFs and it worked.  The only reason I did that is because it was in the sample code I was using to begin with.

    The code that creates the PDF opens a stream

            System.IO.MemoryStream m = new System.IO.MemoryStream();
            iText.Document doc = new iText.Document(iText.PageSize.LETTER);
            iText.pdf.PdfWriter writer = iText.pdf.PdfWriter.GetInstance(doc, m);
            writer.ViewerPreferences = PdfWriter.HideToolbar;

    A bunch of code to create the PDF.

            // Delete the file if it exists.
            if (System.IO.File.Exists(path))
            using (System.IO.FileStream fs = System.IO.File.Create(path))
                fs.Write(content, 0, (int)content.Length);


    Author Closing Comment

    I selected my own comment as part of the solution as it contains useful information.

    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 This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    734 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

    19 Experts available now in Live!

    Get 1:1 Help Now