In this article I will explain with an example, how to export
ASP.Net GridView Arabic,
Persian or
Urdu Language Characters to
PDF using
iTextSharp.
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.
The Button has been assigned with a OnClick event handler.
<asp:GridView ID="gvCustomers" 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="150px" DataField="Name" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="UrduName" HeaderText="Arabic 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 iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Populating GridView with Arabic, Persian or Urdu Characters
Inside the
Page_Load event handler, the
GridView is populated with the records from dynamic
DataTable.
C#
protected void Page_Load(object sender,EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Name"),
new DataColumn("UrduName")
});
dt.Rows.Add("India", "بھارت");
dt.Rows.Add("China", "چین");
dt.Rows.Add("Australia", "آسٹریلیا");
dt.Rows.Add("Nepal", "نیپال");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Name"),
New DataColumn("UrduName")
})
dt.Rows.Add("India", "بھارت")
dt.Rows.Add("China", "چین")
dt.Rows.Add("Australia", "آسٹریلیا")
dt.Rows.Add("Nepal", "نیپال")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Exporting GridView with Arabic, Persian or Urdu 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
Arabic,
Persian or
Urdu 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 btnExportPDF_Click(object sender, EventArgs e)
{
gvCustomers.AllowPaging = false;
gvCustomers.DataBind();
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\Arial.ttf", BaseFont.IDENTITY_H, true);
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);
//Important for Arabic, Persian or Urdu Text.
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
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);
}
//Important for Arabic, Persian or Urdu Text.
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();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub btnExportPDF_Click(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.AllowPaging = False
gvCustomers.DataBind()
Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") & "\fonts\Arial.ttf", BaseFont.IDENTITY_H, True)
Dim table As PdfPTable = 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 Font = New Font(bf, 10, Font.NORMAL)
font.Color = New BaseColor(gvCustomers.HeaderStyle.ForeColor)
Dim cell As PdfPCell = New PdfPCell(New Phrase(12, cellText, font))
'Set Header Row BackGround Color.
cell.BackgroundColor = New BaseColor(gvCustomers.HeaderStyle.BackColor)
'Important for Arabic, Persian or Urdu Text.
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
table.SetWidths(widths)
For i As Integer = 0To 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 Font = New Font(bf, 10, Font.NORMAL)
font.Color = New BaseColor(gvCustomers.RowStyle.ForeColor)
Dim cell As PdfPCell = 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
'Important for Arabic, Persian or Urdu Text.
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
End If
Next
'Create the PDF Document.
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 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
Exported PDF
Demo
Downloads