In this article I will explain with an example, how to export GridView with Arabic, Persian or Urdu Characters to PDF using iTextSharp PDF Conversion library.
HTML Markup
The HTML Markup consists of an ASP.Net GridView which will be populated with Arabic, Persian or Urdu 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="ArabicName" 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 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 Arabic, Persian or Urdu 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("UrduName") });
dt.Rows.Add("India", "بھارت");
dt.Rows.Add("China", "چین");
dt.Rows.Add("Australia", "آسٹریلیا");
dt.Rows.Add("Nepal", "نیپال");
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("UrduName")})
dt.Rows.Add("India", "بھارت")
dt.Rows.Add("China", "چین")
dt.Rows.Add("Australia", "آسٹریلیا")
dt.Rows.Add("Nepal", "نیپال")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Exporting GridView with Arabic, Persian or Urdu Characters to PDF
In the below code, firstly I am fetching the Arial font from the Windows Fonts Directory as the inbuilt iTextSharp Fonts does not properly display the Arabic, Persian or Urdu 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 won’t be rendered 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\Arial.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);
//Important for Arabic, Persian or Urdu Text
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);
//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);
}
//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(sender As Object, e As EventArgs)
GridView1.AllowPaging = False
GridView1.DataBind()
Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + "\fonts\Arial.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)
'Important for Arabic, Persian or Urdu Text
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
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
'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 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
Screenshot
Demo
Downloads