在新标签页C#中打开PDF [英] open PDF in new tab C#

查看:72
本文介绍了在新标签页C#中打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网络上显示PDF文件时在新选项卡中打开它.但不必单击仅在新选项卡中打开文件的功能.这是我的显示功能:

i want to open the PDF file in new tab when i display it on the web. but without clicking just a function that open the file in a new tab. this is my display function:

private void DisplayPage()
    {        
        string path = CanvasWritingStepImages._pdfName;
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(path);

        if (buffer != null)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "inline; filename=Formulike.pdf");
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
        }
        else
        {
            logger.Error("Buffer was Null!");
            throw new Exception("PDF ERROR");
        }
    }

我正在使用"iTextSharp"来处理PDF文件.重要的是,PDF文件将在新选项卡上打开,而无需仅单击该功能即可单击.我刚刚发现点击按钮打开了一个新标签.

i'm using "iTextSharp" to work with the PDF file. it's important that the PDF file will be open on a new tab without any clicking just by the function. i just found with button click that open a new tab.

我尝试了这个:推荐答案

我遇到了非常相似的问题...请参阅我的问题

I had a very similar issue... see my question here.

将pdf创建代码放入新页面的 form load 事件中.然后,使用下面的代码在新标签页中打开该页面.

Put your pdf creation code in the form load event of a new page. Then use the code below to open that page in a new tab.

string url = "myPDFpage.aspx";
string openWindowScript = string.Format("window.open({0}, '_blank');", url);         
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), Guid.NewGuid().ToString(), openWindowScript, true);         

如果您不使用 ScriptManager ,则可以用以下内容替换最后一行:

If you're not using a ScriptManager you can replace the last line with the following:

Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), openWindowScript, true);

如果您的pdf需要创建变量,请将其通过查询字符串传递或将其存储在会话变量中,以便新页面可以访问它们.

If your pdf needs variables to be created, pass them through a query string or store them in a session variable so the new page will have access to them.

除非用户允许从您的站点弹出窗口或关闭其阻止程序,否则新选项卡/窗口将被阻止.

这篇关于在新标签页C#中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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