In this article I will explain with an example, how to export ASP.Net GridView with Devanagari, Hindi or Marathi Characters to PDF using iTextSharp.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

Download iTextSharp Libraries

In order to install iTextSharp library from Nuget, please refer the following articles.
 
 

HTML Markup

The following HTML Markup consists of:
GridView – For displaying data.

Columns

There are two BoundField columns for displaying the fields.
Button – For exporting the GridView to PDF file.
The Button has been assigned with a OnClick event handler.
<asp:GridView ID="GridView1" runat="server" Width="300" HeaderStyle-BackColor="#3AC0F2"
    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="Name" />
        <asp:BoundField ItemStyle-Width="200px" DataField="HindiName" HeaderText="Hindi Name" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="OnExportPDF" />
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
 
 

Populating GridView with Devanagari, Hindi or Marathi Characters

Inside the Page_Load event handler, the GridView is populated with the records from dynamic DataTable.
Note: For more details on how to create DataTable dynamically and bind to GridView, please refer my article Dynamically create DataTable and bind to GridView in ASP.Net
 
C#
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", "जापान");
    gvCustomers.DataSource = dt;
    gvCustomers.DataBind();
}
 
VB.Net
Protected Sub  Page_Load(sender As Object, e As EventArgs)Handles Me.Load
    Dim  dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {
                        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", "जापान")
    gvCustomers.DataSource = dt
    gvCustomers.DataBind()
End Sub
 
 

Exporting GridView with Devanagari, Hindi or Marathi Characters to PDF

When ExportPDF button is clicked, the PDF is exported.
Here, the GridView is again populated with data from database after setting AllowPaging to false.
Then, fetching the Arial font from the Windows Fonts Directory as the inbuilt iTextSharp Fonts does not properly display the Devanagari, Hindi or Marathi Characters.
After that, loop is executed and all the rows and cells from the GridView is inserted into an iTextSharp Table. In each cell of the iTextSharp table, the Background Color, Font color and the Text Direction is set from Right to Left.
Finally, the Table is added to an iTextSharp Document which is then exported to PDF using the Output Response Stream.
C#
protected void OnExportPDF(object sender, EventArgs e)
{
    gvCustomers.AllowPaging = false;
    gvCustomers.DataBind();
 
    BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") +@"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
 
    PdfPTable table = new PdfPTable(gvCustomers.Columns.Count);
    int[] widths = new int[gvCustomers.Columns.Count];
    for (int x = 0; x < gvCustomers.Columns.Count; x++)
    {
        widths[x] = (int)gvCustomers.Columns[x].ItemStyle.Width.Value;
        string cellText = Server.HtmlDecode(gvCustomers.HeaderRow.Cells[x].Text);
 
        //Set Font and Font Color.
        Font font =  new Font(bf, 10, Font.NORMAL);
        font.Color =  new BaseColor(gvCustomers.HeaderStyle.ForeColor);
        PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
 
        //Set Header Row BackGround Color.
        cell.BackgroundColor = new BaseColor(gvCustomers.HeaderStyle.BackColor);
 
        table.AddCell(cell);
    }
    table.SetWidths(widths);
 
    for (int i = 0; i < gvCustomers.Rows.Count; i++)
    {
        if  (gvCustomers.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < gvCustomers.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode(gvCustomers.Rows[i].Cells[j].Text);
 
                //Set Font and Font Color.
                Font font = new Font(bf, 10, Font.NORMAL);
                 font.Color = new BaseColor(gvCustomers.RowStyle.ForeColor);
                PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
 
                //Set Color of row.
                if (i % 2 == 0)
                {
                    //Set Row BackGround Color.
                    cell.BackgroundColor = new BaseColor(gvCustomers.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 */
}
 
VB.Net
Protected Sub OnExportPDF(sender As Object, e As EventArgs)
    gvCustomers.AllowPaging = False
    gvCustomers.DataBind()
 
    Dim bf As BaseFont BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + "\fonts\ARIALUNI.TTF"BaseFont.IDENTITY_H, True)
 
    Dim table As New PdfPTable(gvCustomers.Columns.Count)
    Dim widths As Integer() = New Integer(gvCustomers.Columns.Count - 1) {}
    For x As Integer = 0 To gvCustomers.Columns.Count - 1
        widths(x) = CInt(gvCustomers.Columns(x).ItemStyle.Width.Value)
        Dim cellText As String = Server.HtmlDecode(gvCustomers.HeaderRow.Cells(x).Text)
 
        'Set Font and Font Color.
        Dim font As New Font(bf, 10, Font.NORMAL)
        font.Color = New BaseColor(gvCustomers.HeaderStyle.ForeColor)
        Dim cell As New PdfPCell(New Phrase(12, cellText, font))
 
        'Set Header Row BackGround Color.
        cell.BackgroundColor = New BaseColor(gvCustomers.HeaderStyle.BackColor)
 
        table.AddCell(cell)
    Next
    table.SetWidths(widths)
 
    For i As Integer = 0 To gvCustomers.Rows.Count - 1
        If gvCustomers.Rows(i).RowType = DataControlRowType.DataRow Then
            For j As Integer = 0 To gvCustomers.Columns.Count - 1
                Dim cellText As String = Server.HtmlDecode(gvCustomers.Rows(i).Cells(j).Text)
 
                'Set Font and Font Color.
                Dim font As New Font(bf, 10, Font.NORMAL)
                font.Color = New BaseColor(gvCustomers.RowStyle.ForeColor)
                Dim cell As New PdfPCell(New Phrase(12, cellText, font))
 
                'Set Color of row.
                If i Mod 2 = 0 Then
                    'Set Row BackGround Color.
                    cell.BackgroundColor = New BaseColor(gvCustomers.RowStyle.BackColor)
                End If
                table.AddCell(cell)
            Next
        End If
    Next
 
    'Create the PDF Document.
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.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()
End Sub
 
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered.
End Sub
 
 

Screenshots

The Form

iTextSharp: Export ASP.Net GridView with Devanagari, Hindi or Marathi Characters to PDF
 

Exported PDF

iTextSharp: Export ASP.Net GridView with Devanagari, Hindi or Marathi Characters to PDF
 
 

Demo

 
 

Downloads