Asked by:
Export ASP.Net GridView with Background colors to PDF using iTextSharp

Question
-
User1253526462 posted
Hello Folks,
I wanaa to print gridview in pdf format .print successfull but print without background color, header ,coloumn, and row
And I am using following code :
protected void Button3_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=Govind.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.ShowHeader = true; GridView1.AllowPaging = false; SqlCommand cmd = new SqlCommand("Select top 10 * from tbl_StudentRegistration", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); GridView1.RenderControl(hw); 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", "8px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }
Thanks in Advance.
Thursday, September 27, 2012 11:11 AM
All replies
-
User-271186128 posted
Hi Sir,
As for this issue, I suppose the issue is related to the iTextSharp Library. With reference to the following links, I found that:
By default the iTextSharp Library does not support background color of table cells or table rows
Hence when you render it as PDF your GridView is rendered without any formatting.
You could refer to the following code to add background color.
protected void Button1_Click(object sender, EventArgs e) { GridView1.AllowPaging = false; GridView1.DataBind(); BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true); iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count); int[] widths = new int[GridView1.Columns.Count]; for (int x = 0; x < GridView1.Columns.Count; x++) { widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value; string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text); //Set Font and Font Color iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL); font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor); iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font)); //Set Header Row BackGround Color cell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor); table.AddCell(cell); } table.SetWidths(widths); for (int i = 0; i < GridView1.Rows.Count; i++) { if (GridView1.Rows[i].RowType == DataControlRowType.DataRow) { for (int j = 0; j < GridView1.Columns.Count; j++) { string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text); //Set Font and Font Color iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL); font.Color = new BaseColor(GridView1.RowStyle.ForeColor); iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font)); //Set Color of row if (i % 2 == 0) { //Set Row BackGround Color cell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor); } table.AddCell(cell); } } } //Create the PDF Document Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); pdfDoc.Add(table); pdfDoc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); }
For more details, please refer to this articles:
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
Best Regards,
DillionFriday, March 13, 2015 4:35 AM