出口gridview的PDF文件时更改标题的颜色 [英] Change the Color of header when exporting gridview to PDf

查看:122
本文介绍了出口gridview的PDF文件时更改标题的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GridView导出为PDF。一切正常,但是,当产生的PDF的背景是白色的,标题字体颜色是灰色。这真的很难看到的。我试图出口前更改标题前景色为黑色。它不工作。

I'm exporting gridview to pdf. Everything works, however, when PDF is generated the background is white and the header font color is grey. It's really hard to see. I'm trying to change the header forecolor to black before the export. It's not working.

有什么建议?

  public void ExportToPDF()
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=CallDetail.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        this.GetData();
        GridView1.RenderControl(hw);
        HtmlForm frm = new HtmlForm();
        frm.Attributes["runat"] = "server";
        GridView1.HeaderRow.Style.Add("width", "15%");
        GridView1.HeaderRow.Style.Add("font-size", "10px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "26px");
        for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
        {
            TableCell tc = GridView1.HeaderRow.Cells[col];
            tc.Style.Add("color", "#FFFFFF");
            tc.Style.Add("background-color", "#444");
        }
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();


    }

更新***补充更新code和屏幕截图。

UPDATE*** added updated code and screenshot.

推荐答案

直接设置标头的前景色导出前并没有真正影响headerstyle。可能是另一种方法是遍历所有标题单元格,并设置为任何你需要的样式(本真的作品)。你可以尝试改变下面一行code的

Directly setting the header's ForeColor before export doesn't really affect the headerstyle. May be an alternative is to iterate over all the header cells and set to whatever styles you need (this really works). You can try changing the below line of code

GridView1.HeaderStyle.ForeColor = System.Drawing.Color.Black;

for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
{
    TableCell tc = GridView1.HeaderRow.Cells[col];
    tc.Style.Add("color", "#FFFFFF");
    tc.Style.Add("background-color", "#444");
}

这篇关于出口gridview的PDF文件时更改标题的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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