从内存流中的新标签或窗口打开的字节数组 [英] open byte array from memory stream in new tab or window

查看:120
本文介绍了从内存流中的新标签或窗口打开的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见的​​问题,而是一个漫长的搜索后我还没有找到一个适合我的需求的解决方案。我使用iTextSharp的填写从我创建一个字节数组PDF表单。 (有与我结合成单一的PDF文档相同的信息相同形式多页)。我能做到这一点没有问题,问题是,当我显示最终PDF文档,我需要在不同的窗口或选项卡中打开它没有首先提示用户保存或打开该文件的用户。我也不想有保存在服务器上的文件,并使用FILESTREAM重新打开它。如果我改变了内容处置,以内联的PDF显示在同一个浏览器窗口。这就产生问题,因为不是有点击浏览器的后退按钮,用户导航回其重新启动表单提交过程中的网站。因为它目前是,我的code成功生成PDF和促使他们打开或保存它。这是一步我需要删除。我似乎记得,在一个新的标签或窗口中打开的PDF一些Java片段,但我一直在复制/发现它不成功。我也打了不同的页眉,但我已经碰了壁。任何帮助将大大AP preciated。

(我把我的$ C $从以下必要的数据验证后,点击一个按钮C的方法去。)

 私人无效围棋()
    {
        清单<字节[]> PDF文件=新的List<字节[]>();        而(的PageNumber< = Convert.ToInt32(PageCountLabel.Text))
        {
            pdfs.Add(PopulatePDF());
        }        的MemoryStream毫秒​​= MergePDFs(PDF文件);
            //打开PDF在新标签后保存/打开选项
        Response.AddHeader(内容处置,附件;文件名= TheDocument.pdf);
        Response.ContentType =应用程序/ PDF
        Response.BinaryWrite(ms.ToArray());
        到Response.End();
    }
    // ************ PDF格式填写**************** //    私人字节[] PopulatePDF()
    {        MemoryStream的毫秒=新的MemoryStream();
        PdfStamper印模= NULL;
        PdfReader读卡器=新PdfReader(使用Server.Mappath(〜/ PDFTemplates / template1.pdf));
        尝试
        {
            字符串TEMP = Profile.ToString()ToUpper的()。            PdfCopyFields复印机=新PdfCopyFields(毫秒);
            Copier.AddDocument(阅读器);
            Copier.Close();            PdfReader文件读屏=新PdfReader(ms.ToArray());
            MS =新的MemoryStream();
            印模=新PdfStamper(文件阅读器,MS);
            AcroFields字段= Stamper.AcroFields;            //此处填写表单字段            的PageNumber ++;
            Stamper.FormFlattening = TRUE;
        }
        最后
        {
            如果(印模!= NULL)
            {
                Stamper.Close();
            }
        }
        返回ms.ToArray();
    }
    //结合的PDF页面到单一的文件
    私人的MemoryStream MergePDFs(列表<字节[]> PDF文件)
    {
        MemoryStream的毫秒=新的MemoryStream();
        PdfCopyFields复印机=新PdfCopyFields(毫秒);        的foreach(在PDF中VAR PDF)
            Copier.AddDocument(新PdfReader(PDF));
        Copier.Close();
        返回毫秒;
    }


解决方案

把你的PDF生成code在一个新的页面MyPDFPage.aspx上的网址参数,例如按钮原来的页面上有一个onclick

:使用 window.open()从JavaScript 事件

 < HTML和GT;
< HEAD>
<脚本>
功能open_win()
{
    window.open(MyPDFPage.aspx?FILEID = 0001,_blank)
}
< / SCRIPT>
< /头>
<身体GT;<输入类型=按钮值=打开窗口的onclick =open_win()>< /身体GT;
< / HTML>

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

解决方案

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>

这篇关于从内存流中的新标签或窗口打开的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆