How to save a document to database using MemoryStream or Response.OutputStream?

Posted on 2008-11-06
Last Modified: 2012-05-05
hi all,
i have a gridview, when i select one row and click the button A, it will generate a PDF document, save it to database and popup/display it to users.  how to achieve it?

thanks a lot
Response.ContentType = "application/pdf";

        Response.AddHeader("Content-Disposition", "inline;filename=EmailConfirmation.pdf");

        MemoryStream stream = new MemoryStream();

        InitPDF(stream ); 

foreach (GridViewRow row in gvEmail.Rows)


            CheckBox cb = (CheckBox)row.FindControl("cbLetter");

            if (cb.Checked)


                AddPage(row); //generate PDF file



        stream.WriteTo(Response.OutputStream); //if i use this method, it pops up a window to ask me to save the file. but i don't want to do this, i want to directly pops up the file to display it to user. how to do that?





private void InitPDF(Stream stream)



        Arial_8 = FontFactory.GetFont("Arial", 8, Font.NORMAL);

        Arial_9_bold = FontFactory.GetFont("Arial", 9, Font.BOLD);

        Arial_12 = FontFactory.GetFont("Arial", 12, Font.NORMAL);

        Arial_12_bold = FontFactory.GetFont("Arial", 12, Font.BOLD);

        Arial_12_underline = FontFactory.GetFont("Arial", 12, Font.UNDERLINE);

        document = new Document(PageSize.A4, 60, 60, 60, 90);

        writer = PdfWriter.GetInstance(document, stream);

        writer.PageEvent = new PDFTemplate(document, true, false, false);

        writer.SetEncryption(true, null, "aaa", PdfWriter.AllowPrinting);	// disable copy




        document.AddTitle("Email Confirmation");



Open in new window

Question by:viola123
    LVL 26

    Expert Comment

    by:Anurag Thakur
    hi regarding the PDF popping up the dialog see the following links

    saving the pdf file to sql server check the follwoing links
    the link shows how to save an image file to the sql server but the same can be converted to pdf too
    LVL 6

    Author Comment

    i tried the solutions, they do not work for me. the file download window is still be popped up to ask you to save the PDF file.
    LVL 6

    Accepted Solution

    now i have to create two documents, one is used to save to database(this one using MemoryStream), the other one is used to pop up(this one using Response.OutputStream). it works quite well.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now