In this article I will explain how to export GridView with Devanagari, Hindi or Marathi Characters to PDF using iTextSharp PDF Conversion library.
HTML Markup
The HTML Markup consists of an ASP.Net GridView which will be populated with Devanagari, Hindi or Marathi Content and a Button that will export the GridView to PDF
<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="Name" />
<asp:BoundField ItemStyle-Width="200px" DataField="HindiName" HeaderText="Hindi Name" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="btnExportPDF_Click" />
Namespaces
You will need to import the following namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.Text
Populating GridView with Devanagari, Hindi or Marathi Characters
Here I am making use of a DataTable with dummy content to populate the GridView, but you can make use of database too.
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", "जापान");
GridView1.DataSource = dt;
GridView1.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", "जापान")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Exporting GridView with Devanagari, Hindi or Marathi Characters to PDF
In the below code, firstly I am fetching the ArialUni font from the Windows Fonts Directory as the inbuilt iTextSharp Fonts does not properly display the Devanagari, Hindi or Marathi Characters.
Then a 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
Note: The iTextSharp DLL supplied with the code sample of this article has been modified to allow GridView Styles, Colors and Formatting. Thus if you use any other copy of iTextSharp the GridView Styles, Colors and Formatting might not render in the exported PDF.
C#
protected void btnExportPDF_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 Color(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 Color(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 Color(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 Color(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 */
}
VB.Net
Protected Sub btnExportPDF_Click(sender As Object, e As EventArgs)
GridView1.AllowPaging = False
GridView1.DataBind()
Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + "\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, True)
Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count)
Dim widths As Integer() = New Integer(GridView1.Columns.Count - 1) {}
For x As Integer = 0 To GridView1.Columns.Count - 1
widths(x) = CInt(GridView1.Columns(x).ItemStyle.Width.Value)
Dim cellText As String = Server.HtmlDecode(GridView1.HeaderRow.Cells(x).Text)
'Set Font and Font Color
Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
font.Color = New Color(GridView1.HeaderStyle.ForeColor)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
'Set Header Row BackGround Color
cell.BackgroundColor = New Color(GridView1.HeaderStyle.BackColor)
table.AddCell(cell)
Next
table.SetWidths(widths)
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
For j As Integer = 0 To GridView1.Columns.Count - 1
Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)
'Set Font and Font Color
Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
font.Color = New Color(GridView1.RowStyle.ForeColor)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
'Set Color of row
If i Mod 2 = 0 Then
'Set Row BackGround Color
cell.BackgroundColor = New Color(GridView1.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
Demo
Downloads