导出为PDF功能不提供在浏览器上保存或打开的选项

时间:2016-06-06 13:30:11

标签: c# asp.net vb.net pdf-generation

public string ExportToPDF_ManageDefects(bool isEmailPDF, DataTable dt, string headerText)
    {

        string timeStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
        string pdfName = string.Empty;
        //string headerText = string.Empty;
        //Create document
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        PdfWriter writer = null;
        FileStream fs =null;
        try
        {
            if (!isEmailPDF)
            {
                pdfName = headerText+"_" + timeStamp + ".pdf";
                fs = new FileStream(HttpContext.Current.Server.MapPath("~/PDFReport/"+pdfName), FileMode.Create, FileAccess.Write, FileShare.None);
                writer = PdfWriter.GetInstance(document, fs);
            }
            else
            {
                writer = PdfWriter.GetInstance(document, HttpContext.Current.Response.OutputStream);
            }

            DataTable filteredData = dt;// (DataTable)HttpContext.Current.Session["filteredData"];
            document.Open();
            document.Add(CreatePDFTable_HeaderImage());
            document.Add(CreatePDFTable_HeaderText(headerText));
            document.Add(CreatePDFTable_SearchText(headerText));
            document.Add(CreatePDFTable_BodyDataTable(filteredData));
            document.Add(CreatePDFTable_Footer());
            document.Close();

            if (isEmailPDF)
            {
                HttpContext.Current.Response.ContentType = "application/PDF";
                HttpContext.Current.Response.AddHeader("Content-Disposition:", "attachment; filename='"+headerText+"'" + timeStamp + ".pdf");
                HttpContext.Current.Response.Write("<script>window.open('"+headerText+"_" + timeStamp + ".pdf',target='new');</script>");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.End();
            }
        }
        catch (Exception ex)
        {

        }
        return pdfName;

在我的情况下,我试图在浏览器上弹出保存或打开窗口,其中物理文件保存在上述路径中。

要求是在新标签页中打开,否则至少应该要求保存或打开

我也用过 到Response.End(); // - 尝试将此行更改为 - &gt;

HttpContext.Current.ApplicationInstance.CompleteRequest(); // - 这不会抛出异常,但对我不起作用。

参数如下:IsEmail = False,headerText = GroupDetails_201666185411,dt = DataTable的设置。

enter image description here

1 个答案:

答案 0 :(得分:0)

在我的html页面中,我刚刚添加了这段代码,我的工作已经完成。

<Triggers>
            <asp:PostBackTrigger ControlID="btnPDF" />
            <asp:PostBackTrigger ControlID="btnCSV" />
        </Triggers>

之后我编写了JavaScript代码以在新标签页中打开我的PDF文件。

function NewWindow() {
        document.forms[0].target = "_blank";
    }

在我的按钮事件中调用相同的函数。

<asp:Button ID="btnPDF" runat="server" CssClass="BigBtn" OnClientClick="NewWindow();"></asp:Button>

全部感谢=)