4

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?

Mojingo
  • 43
  • 1
  • 4

3 Answers3

3

iTextSharp is a better choice for RTL languages. You can find a tutorial about it here.

VahidN
  • 18,457
  • 8
  • 73
  • 117
3

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();
    }
}
Yaniv
  • 31
  • 5
1

PDFsharp does not (yet) support right-to-left languages.

See also: http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx#Does_PDFsharp_support_for_Arabic_Hebrew_CJK_Chinese_Japanese_Korean_6