I am generating a report from Unity3d with iTextSharp,
Problem is showing arabic characters. There is one XML file from where I am parsing some arabic text, and when I put it in PDF, it shows well. But when I put normal string in MonoDevelop, and try to show it in PDF, it shows some weird characters.
Here is my code:
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 20, 20);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("TestPDF.pdf", FileMode.Create));
doc.Open();
string fontPath = Application.dataPath + "\\Fonts\\Arial.ttf";
// string fontPath = Application.dataPath + "\\Fonts\\arabtype.ttf";
BaseFont basefont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
string tt = GameObject.Find("TextPlay").GetComponent<TextMesh>().text; //parsed text
iTextSharp.text.Font normalFont = new iTextSharp.text.Font(basefont, 35, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
PdfPTable tableUnit = new PdfPTable(2);
tableUnit.DefaultCell.NoWrap = false;
tableUnit.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
tableUnit.DefaultCell.Border = Rectangle.NO_BORDER;
tableUnit.SpacingBefore = 30f;
tableUnit.KeepTogether = true;
PdfPCell questionOneCell = new PdfPCell(new Phrase("نتائجك", normalFont));
questionOneCell.NoWrap = false;
questionOneCell.BorderWidth = 0;
questionOneCell.BackgroundColor = nColor;
questionOneCell.HorizontalAlignment = Element.ALIGN_CENTER;
questionOneCell.VerticalAlignment = Element.ALIGN_CENTER;
tableUnit.AddCell(questionOneCell);
doc.Add(tableUnit);
doc.Close();
I did try several fonts, but nothing changes.
Please take a look at the first part of my answer to the following question: Can't get Czech characters while generating a PDF
Allow me to copy/paste that part in answer to your question:
You are writing code that contains special characters:
"Testing of letters Č,Ć,Š,Ž,Đ"
That is a bad practice. Code files are stored as plain text and can be saved using different encodings. An accidental switch from encoding (for instance: by uploading it to a versioning system that uses a different encoding), can seriously damage the content of your file.
You should write code that doesn't contain special characters, but that use a different notations. For instance:
"Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110"
This will also make sure that the content doesn't get altered when compiling the code using a compiler that expects a different encoding.
You are experiencing the same problem:
Using "نتائجك"
in your source code is bad programming practice. You should always put literal string in your code using the unicode notation. If you don't do that and you save the file using the wrong encoding (e.g by uploading it to a versioning system) or if you compile the file using the wrong encoding or if you execute the file in an environment using the wrong encoding, then you run into problems.
Better yet:
Never store hard-coded values in your source code. Use properties files and text files instead. Make sure the files are stored and read as UTF-8.