Hello again,
I found the solution finally.
I added the line
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
just before adding the cell to the table
table.AddCell(cell);
so the full code is
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();
}