【问题标题】:Export aspx web pages to pdf and download将 aspx 网页导出为 pdf 并下载
【发布时间】:2014-01-09 02:06:33
【问题描述】:

我在尝试将 aspx 页面导出为 pdf 并下载时遇到了一些问题。代码如下:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string url = Request.Url.AbsoluteUri;
        using (StreamReader reader = new StreamReader(Server.MapPath(url)))
        {
            String line = reader.ReadToEnd();
            Text2PDF(line);
        }
    }

    protected void Text2PDF(string PDFText)
    {
        //HttpContext context = HttpContext.Current;
        StringReader reader = new StringReader(PDFText);

        //Create PDF document 
        Document document = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(document);

        string PDF_FileName = Server.MapPath("MyFirstPdf.pdf");
        PdfWriter.GetInstance(document, new FileStream(PDF_FileName, FileMode.Create));
        document.Open();

        try
        {
            parser.Parse(reader);
        }
        catch (Exception ex)
        {
            //Display parser errors in PDF. 
            Paragraph paragraph = new Paragraph("Error!" + ex.Message);
            Chunk text = paragraph.Chunks[0] as Chunk;
            if (text != null)
            {
                text.Font.Color = BaseColor.RED;
            }
            document.Add(paragraph);
        }
        finally
        {
            document.Close();
            DownLoadPdf(PDF_FileName);
        }
    }
    private void DownLoadPdf(string PDF_FileName)
    {
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(PDF_FileName);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }

但是,当我单击按钮时,没有任何反应。浏览器也不会提示我下载。我想知道为什么。提前致谢。

【问题讨论】:

    标签: c# asp.net pdf


    【解决方案1】:

    您是否将触发器添加到您的 aspx 文件中。如果不是,请添加触发器作为示例

    触发器>

    【讨论】:

    • 我相信您在页面中使用了 UpdatePanel
    • 你的意思是在我在 aspx 文件中声明我的按钮之后添加上面的行。不,我没有使用任何更新面板
    • 这取决于您,因为您必须使用适当的代码来创建 pdf 文件
    • 使用 itext 清晰。有关更多详细信息,请查看此代码stackoverflow.com/questions/4387352/…
    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2012-05-11
    • 2019-06-28
    • 2021-07-05
    相关资源
    最近更新 更多