I use iTextSharp to print a grid view but I face some problems:
No arabic characters appears at all.
The direction is LTR and I wany it RTL
In the out put you can see that English Columns on LTR and Arabic Column on RTL. if you want to display GridView on page with RTL than you can use <div dir="rtl">Put the Grid View here</div>
.
GridView:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="العربية" SortExpression="العربية">
<ItemTemplate>
<asp:Label ID="arabic" runat="server" Text='<%# Eval("العربية")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="English" SortExpression="English">
<ItemTemplate>
<asp:Label ID="english" runat="server" Text='<%# Eval("English") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="open" runat="server" Text="Open" NavigateUrl="~/Default.aspx"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [English], [العربية] FROM [Test]"></asp:SqlDataSource>
<br />
<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export to PDF" />
CS Code:
protected void ExportPDF(GridView gridViewReport)
{
int columnNumber = 0, rowNumber = 0;
DataTable tbl = null;
if (gridViewReport.AutoGenerateColumns)
{
tbl = gridViewReport.DataSource as DataTable; // Gets the DataSource of the GridView Control.
columnNumber = tbl.Columns.Count;
rowNumber = tbl.Rows.Count;
}
else
{
columnNumber = gridViewReport.Columns.Count;
rowNumber = gridViewReport.Rows.Count;
}
// Creates a PDF document
Document document = null;
document = new Document(PageSize.A4, 0, 0, 15, 5);
PdfPTable _table = new PdfPTable(GridView1.Columns.Count);
_table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
BaseFont baseFont = BaseFont.CreateFont("c:\\\\windows\\\\fonts\\\\tahoma.ttf", BaseFont.IDENTITY_H, true); // Font which has Arabic characters
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 10, iTextSharp.text.Font.NORMAL);
// Sets the gridview column names as table headers.
for (int i = 0; i < columnNumber; i++)
{
iTextSharp.text.pdf.PdfPCell ph = null;
if (gridViewReport.AutoGenerateColumns)
{
ph = new PdfPCell(new Phrase(10, tbl.Columns[i].ColumnName, font));
}
else
{
ph = new PdfPCell(new Phrase(10, gridViewReport.Columns[i].HeaderText, font));
}
if (ph != null && gridViewReport.Columns[i].HeaderText != "")
{
if (Regex.IsMatch(gridViewReport.Columns[i].HeaderText, "^[a-zA-Z0-9 ]*$")) // Check if Header Text is English
{
ph.RunDirection = PdfWriter.RUN_DIRECTION_LTR; // Left to Right
BaseColor color = new BaseColor(Color.Red);
ph.BackgroundColor = color;
_table.AddCell(ph);
}
else
{
ph.RunDirection = PdfWriter.RUN_DIRECTION_RTL; //Right to Left
BaseColor color = new BaseColor(Color.Red);
ph.BackgroundColor = color;
_table.AddCell(ph);
}
}
else
{
ph.Border = iTextSharp.text.Rectangle.NO_BORDER;
_table.AddCell(ph);
}
}
// Get the gridview rows and adds them to the _table
for (int rowIteration = 0; rowIteration < rowNumber; rowIteration++)
{
for (int columnIteration = 0; columnIteration < columnNumber; columnIteration++)
{
if (gridViewReport.AutoGenerateColumns) //Check if AutoGenrated Colunms
{
string s = gridViewReport.Rows[rowIteration].Cells[columnIteration].Text.Trim();
PdfPCell ph = new PdfPCell(new Phrase(10, s, font));
_table.AddCell(ph);
}
else
{
if (gridViewReport.Columns[columnIteration] is TemplateField) // Check if Item Template
{
PdfPCell ph = null;
Label lc = gridViewReport.Rows[rowIteration].Cells[columnIteration].Controls[1] as Label; // Label
Button btn = gridViewReport.Rows[rowIteration].Cells[columnIteration].Controls[1] as Button;// Button
HyperLink hyperLink = gridViewReport.Rows[rowIteration].Cells[columnIteration].Controls[1] as HyperLink; // HyperLink
if (lc != null)
{
string s = lc.Text.Trim();
ph = new PdfPCell(new Phrase(10, s, font));
if (Regex.IsMatch(s, "^[a-zA-Z0-9 ]*$")) //Check if cell string is English
{ ph.RunDirection = PdfWriter.RUN_DIRECTION_LTR; // Left to Right
_table.AddCell(ph);
}
else
{
ph.RunDirection = PdfWriter.RUN_DIRECTION_RTL; // Right to Left
_table.AddCell(ph);
}
}
else if (btn != null)
{
ph = new PdfPCell(new Phrase(10, btn.Text, font));
ph.Phrase.Clear(); // Clear the Cell Phrase
ph.Border = iTextSharp.text.Rectangle.NO_BORDER;
_table.AddCell(ph);
}
else if (hyperLink != null)
{
ph = new PdfPCell(new Phrase(10, hyperLink.Text, font));
ph.Phrase.Clear(); //Clear the Cell Phrase
ph.Border = iTextSharp.text.Rectangle.NO_BORDER;
_table.AddCell(ph);
}
else
{
_table.AddCell("--");
}
}
else
{
string s = gridViewReport.Rows[rowIteration].Cells[columnIteration].Text.Trim();
PdfPCell ph = new PdfPCell(new Phrase(10, s, font));
_table.AddCell(ph);
}
}
}
_table.CompleteRow();
}
PdfWriter.GetInstance(document, Response.OutputStream);
// Opens the document.
document.Open();
// Adds the _table to the document.
document.Add(_table);
// Closes the document.
document.Close();
Page.Response.ContentType = "application/pdf";
Page.Response.AddHeader("content-disposition", "attachment;filename=MyGrid.pdf");
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Page.Response.Write(document);
Page.Response.End();
}
This code might not be the perfectly coded but I hope this will help you to get your required results.
This GridView exporting library supports RTL. Also you can try PdfReport library as well.