如何以pdf格式下载当前页面 [英] How to download current page in pdf format

查看:113
本文介绍了如何以pdf格式下载当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   代码

< div style = font-family:Arial; background-color:black;> This 测试页< / div >
< div>
< table border = 1 width = 100 >
< tr>
< td>名称< / td >
< td>年龄< / td >
< / tr >
< tr>
< td> John < / td >
< td> 11 < / td >
< / tr >
< tr>
< td> Sam < / td >
< td> 13 < / td >
< / tr >
< tr>
< td> Tony < / td >
< td> 12 < / td >
< / tr >
< / table >
< / div >
< div>
< asp:按钮ID = btnExport runat = server Text = 导出 OnClick = btnExport_Click />
< / div >



背后的代码是 this

protected void btnExport_Click( object sender,EventArgs e)
{

Response.ContentType = 应用/ PDF;
Response.AddHeader( content-disposition attachment; filename = TestPage.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this .Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc = 文件(PageSize.A4,10f,10f,100f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}

文件 下载但背景颜色我已经 div style = font-family:Arial; background-color:black;>此 一个测试页< / div >
未来问题!! ??

解决方案





尝试以下代码。如果它结算,请将其标记为答案。



http://forums.asp.net/t/ 1670886.aspx [ ^ ]

this is code

 <div style="font-family: Arial; background-color:black;">This is a test page</div>
    <div>
        <table border="1" width="100">
            <tr>
                <td>Name</td>
                <td>Age</td>
            </tr>
            <tr>
                <td>John</td>
                <td>11</td>
            </tr>
            <tr>
                <td>Sam</td>
                <td>13</td>
            </tr>
            <tr>
                <td>Tony</td>
                <td>12</td>
            </tr>
        </table>
    </div>
    <div>
        <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
    </div>



and code behind is this

  protected void btnExport_Click(object sender, EventArgs e)
        {

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.Page.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }

file is getting downloaded but background color which i have given in div style="font-family: Arial; background-color:black;">This is a test page</div>
is not coming what is the issue !!??

解决方案

Hi

Try the below code . Mark it as answer if it resolves.

http://forums.asp.net/t/1670886.aspx[^]


这篇关于如何以pdf格式下载当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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