Gridview header text not taking hindi font when export to pdf
Hi,I am tying to export gridview into pdf. But problem is that my
my row display hind text but header text is bot attacepting
hindi properly.
<asp:UpdatePanel ID="upmain" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btnExportPDF" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width="300" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White"
RowStyle-ForeColor="#3A3A3A" AutoGenerateColumns="false" Font-Names="Arial" Font-Size="12">
<Columns>
<asp:BoundField ItemStyle-Width="200px" DataField="Name" HeaderText="?????? ????." />
<asp:BoundField ItemStyle-Width="200px" DataField="HindiName" HeaderText="Hindi Name" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="btnExportPDF_Click" />
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("HindiName") });
dt.Rows.Add("India", "????");
dt.Rows.Add("China", "???");
dt.Rows.Add("England", "???????");
dt.Rows.Add("Canada", "?????");
dt.Rows.Add("Russia", "???");
dt.Rows.Add("Japan", "?????");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\MANGAL.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 iTextSharp.text.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 iTextSharp.text.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 iTextSharp.text.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 iTextSharp.text.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();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}