open byte array from memory stream in new tab or window

旧街凉风 提交于 2019-12-06 13:40:28

问题


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;
    }

回答1:


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>


来源:https://stackoverflow.com/questions/14509678/open-byte-array-from-memory-stream-in-new-tab-or-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!