First of all I am sorry for my language grammar because my first language is Persian (Iran).
I want to add Persian text to the PDF headline, but when I do this through the iTextSharp.text.Paragraph class, I get into trouble.
I can solve this problem with the iTextSharp.text.pdf.ColumnText class but I can't exactly put the text alignment in the center.((PageSize.Left + PageSize.Right)/2)-((TextRenderer.MeasureText("تاریخ گزارش", font).Width)/2), but it's not exactly in the center.
These are my codes:
public string LatinDigitToPersianDigit(string Text)
{
string[] Latin = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string[] Persian = new string[10] { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
for (byte i = 0; i <= 9; i++)
{
Text = Text.Replace(Latin[i], Persian[i]);
}
return Text;
}
void AddPageNumber(string FileIn, string FileOut)
{
byte[] Bytes = File.ReadAllBytes(FileIn);
BaseFont BaseFont = BaseFont.CreateFont(System.Windows.Forms.Application.StartupPath + @"\Font Collection\Bahij Uthman Taha.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font PageNumberFont = new Font(BaseFont, 12);
using (MemoryStream Stream = new MemoryStream())
{
PdfReader PDF_Reader = new PdfReader(Bytes);
iTextSharp.text.Rectangle PageSize = PDF_Reader.GetPageSize(1);
using (PdfStamper Stamper = new PdfStamper(PDF_Reader, Stream))
{
for (int i = 1; i <= PDF_Reader.NumberOfPages; i++)
{
iTextSharp.text.pdf.ColumnText.ShowTextAligned(Stamper.GetUnderContent(i), Element.ALIGN_CENTER, new Phrase(LatinDigitToPersianDigit(i.ToString()), PageNumberFont), (PageSize.Left + PageSize.Right) / 2, 15f, 0);
}
}
Bytes = Stream.ToArray();
}
File.WriteAllBytes(FileOut, Bytes);
}
public void ExportToPdf(DataGrid dataGrid, DataTable DT, byte VisualColumnIndex)
{
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter PDF_Writer = PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Export.pdf", FileMode.Create));
Doc.Open();
PDF_Writer.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
BaseFont BaseFont = BaseFont.CreateFont(System.Windows.Forms.Application.StartupPath + @"\Font Collection\Bahij Uthman Taha.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font DT_Font = new Font(BaseFont, 9);
PdfPTable PDF_Table = new PdfPTable(DT.Columns.Count);
PDF_Table.WidthPercentage = 100;
PDF_Table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
foreach (DataGridColumn Column in dataGrid.Columns)
{
PDF_Table.AddCell(new Phrase(Column.Header.ToString(), DT_Font));
}
byte[] ImageByte = null;
iTextSharp.text.Image CellImage = null;
foreach (DataRow r in DT.Rows)
{
for (int i = 0; i < DT.Columns.Count; i++)
{
if (i == VisualColumnIndex)
{
ImageByte = (byte[])r[i];
CellImage = iTextSharp.text.Image.GetInstance(ImageByte);
PDF_Table.AddCell(CellImage);
}
else
{
PDF_Table.AddCell(new Phrase(LatinDigitToPersianDigit(r[i].ToString()), DT_Font));
}
}
}
string T = '\u202C' + "سلام";
iTextSharp.text.Paragraph PG = new iTextSharp.text.Paragraph(T,DT_Font);
PG.Alignment = Element.ALIGN_CENTER;
PG.SpacingAfter = 20;
Doc.Add(PG);
Doc.Add(PDF_Table);
Doc.Close();
AddPageNumber(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Export.pdf", Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Export.pdf");
}
Output:


Thanks