Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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();
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top