Reputation: 43
I'm working with PdfSharp to create some pdf files. Everything works fine except when I try to put some text into the file in a right-to-left language (e.g., Persian) using Drawstring method. Although I choose Unicode encoding in XPdfFontOptions and a suitable font family (e.g., "B Nazanin"), it draws the letters discretely.
Here is an image of what I get.
B.T.W, is there any better way to create pdf files?
Upvotes: 4
Views: 3086
Reputation: 31
You need to reverse the letters and then the whole string. I needed it myself so its tested and working:
public static string ReverseString(this string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str.Reverse())
{
sb.Append(c);
}
return sb.ToString();
}
public static string RightToLeft(this string str)
{
List<string> output = str.Split(' ').Select(s => s.Any(c => c >= 1424 && c <= 1535) ? s.ReverseString() : s).ToList();
output.Reverse();
return string.Join(" ", output.ToArray());
}
private void DrawStringBoxRightToLeft(XGraphics gfx, string text, XFont font, XBrush brush, XRect rect)
{
List<string> words = text.Split(' ').ToList();
List<string> sentences = new List<string>();
while (words.Any())
{
while (gfx.MeasureString(string.Join(" ", sentences), font).Width < rect.Width && words.Any())
{
string s = words[0];
sentences.Add(s);
words.RemoveAt(0);
}
gfx.DrawString(string.Join(" ", sentences).RightToLeft(), font, brush, rect, XStringFormats.TopRight);
rect.Y += font.Height;
sentences.Clear();
}
}
Upvotes: 3
Reputation: 21729
PDFsharp does not (yet) support right-to-left languages.
Upvotes: 1