Export GridView Arabic or Persian Language Content to PDF using iTextSharp in ASP.Net

Last Reply on Dec 29, 2012 01:33 AM By Mudassar

Posted on Dec 27, 2012 12:32 AM

hi,

i have a problem,when i export grideview to pdf,not show coulmname with formate farsi why????

please helpme.........

Posted on Dec 29, 2012 01:33 AM Modified on on Apr 12, 2013 09:48 AM

Check this sample it works for me

 

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns = "false" Font-Names = "Arial" 
        Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
        HeaderStyle-BackColor = "green" >
       <Columns>
        <asp:BoundField ItemStyle-Width = "200px" DataField = "Name" HeaderText = "Name" />
        <asp:BoundField ItemStyle-Width = "200px" DataField = "ArabicName" HeaderText = "Arabic Name" />
       </Columns> 
    </asp:GridView>

 

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("ArabicName") });
        dt.Rows.Add("John", "اشراق خان‬‎");
        dt.Rows.Add("Rick", "اشراق خان‬‎");
        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\tahoma.ttf", BaseFont.IDENTITY_H, true);
        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
        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);
            iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
            cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#008000"));
           cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            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);
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
                    //Set Color of Alternating row
                    if (i % 2 != 0)
                    {
                        cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B"));
                    }
                   cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                   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();
    }