open byte array from memory stream in new tab or window

11,318

Put your PDF generation code in a new page MyPDFPage.aspx with parameters on the URL, then for example a button on your original page with an onclick event that uses window.open() from javascript:

<html>
<head>
<script>
function open_win()
{
    window.open("MyPDFPage.aspx?fileid=0001", "_blank")
}
</script>
</head>
<body>

<input type="button" value="Open Window" onclick="open_win()">

</body>
</html>
Share:
11,318
ovaltein
Author by

ovaltein

I'm a .Net programmer who might secretly be Batman. (No one has ever actually seen us in the same room together, so it's possible). I'm also into board games so you may find me lurking in Board and Card Games in addition to Stack Overflow.

Updated on June 15, 2022

Comments

  • ovaltein
    ovaltein almost 2 years

    This seems to be a common problem but after a lengthy search I have yet to find a solution that fits my needs. I am using itextsharp to fill out a pdf form from which i create a byte array. (There are multiple pages of the same form with the same information that I combine into a single pdf document). I can do this without issues, the problem is when I display the final pdf document to the user I need to open it in a different window or tab WITHOUT first prompting the user to save or open the file. I also do NOT want to have to save a file on the server and reopen it using filestream. If I change the content-disposition to "inline" the pdf is displayed in the same browser window. This creates problems because the user than has to click the browser back button to navigate back to the site which restarts the form submission process. As it currently sits, my code successfully generates the pdf and prompts them to open or save it. This is the step I need to remove. I seem to remember some java snippet that opened the pdf in a new tab or window but I have been unsuccessful in replicating / finding it. I've also played with different headers but I've hit a wall. Any help would be greatly appreciated.

    (I call the go method of my code below from a button click after necessary data validation.)

    private void go()
        {
            List<byte[]> pdfs = new List<byte[]>();
    
            while (PageNumber <= Convert.ToInt32(PageCountLabel.Text))
            {
                pdfs.Add(PopulatePDF());
            }
    
            MemoryStream ms = MergePDFs(pdfs);
                //opens pdf in new tab after save/open option
            Response.AddHeader("Content-Disposition", "attachment; filename=TheDocument.pdf");
    
    
            Response.ContentType = "application/pdf";            
            Response.BinaryWrite(ms.ToArray());
            Response.End();                             
        }
    
    
        //************fills in pdf form****************//
    
        private byte[] PopulatePDF()
        {         
    
            MemoryStream ms = new MemoryStream();
            PdfStamper Stamper = null;            
            PdfReader Reader = new PdfReader(Server.MapPath("~/PDFTemplates/template1.pdf"));
            try
            {
                string temp = Profile.ToString().ToUpper();
    
                PdfCopyFields Copier = new PdfCopyFields(ms);
                Copier.AddDocument(Reader);
                Copier.Close();
    
                PdfReader docReader = new PdfReader(ms.ToArray());
                ms = new MemoryStream();
                Stamper = new PdfStamper(docReader, ms);
                AcroFields Fields = Stamper.AcroFields;
    
                //fill form fields here                                         
    
                PageNumber++;
                Stamper.FormFlattening = true;
            }
            finally
            {
                if (Stamper != null)
                {
                    Stamper.Close();
                }
            }
            return ms.ToArray();
        }
    
    
        //combines pdf pages into single document
        private MemoryStream MergePDFs(List<byte[]> pdfs)
        {
            MemoryStream ms = new MemoryStream();
            PdfCopyFields Copier = new PdfCopyFields(ms);
    
            foreach (var pdf in pdfs)
                Copier.AddDocument(new PdfReader(pdf));
            Copier.Close();
            return ms;
        }
    
  • Christopher Klein
    Christopher Klein over 11 years
    Basically doing the exact same thing Steve is to generate the PDF and this is more or less exactly what we did to display it in its own page :)
  • ovaltein
    ovaltein over 11 years
    Saw something similar during my search but I guess my brain didn't connect the dots :) This works just like I want, Thank You!