Cannot print Arabic/Persian letters correctly in C#

Viewed 235

I have problems with printing typing Arabic letters in C# using printdocument.

Here's my code:

PrintDocument pd;
PaperSize ps;

void pd_Factor(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    Font vazir = new Font("Vazir Code FD", 12, FontStyle.Regular);
    SolidBrush sb = new SolidBrush(Color.Black);
    string a = "سلام";
    g.DrawString(a, vazir, sb, 200, 330);
}

private void btnDone_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    PaperSize ps = new PaperSize("Factor", 723, 1024);
    pd.PrintPage += new PrintPageEventHandler(pd_Factor);
    pd.PrintController = new StandardPrintController();
    pd.DefaultPageSettings.Margins.Left = 0;
    pd.DefaultPageSettings.Margins.Right = 0;
    pd.DefaultPageSettings.Margins.Top = 0;
    pd.DefaultPageSettings.Margins.Bottom = 0;

    pd.DefaultPageSettings.PaperSize = ps;
    printDialog1.Document = pd;
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            pd.Print();
        }
        catch (Exception)
        {
            
        }
    }
}

Sadly, the above code prints this way:

By the way, I tried

StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
g.DrawString(a, vazir, sb, 200, 330, format);

It just make the position of anchor going from up-left to up-right this way:

So, I thought with myself, I should reverse it:

string a = "سلام";
string b = "";
for (int i = a.Length - 1; i > -1; i--)
{
    b += Convert.ToString(a[i]);
}

And it made the text looks like this:

It still looks wrong but it goes better.

However I tried adding characters from left-to-right using character map.

And my code changed to:

string a = "ﻡﺎﻠﺳ";

And it prints correctly:

By the way, I have an input and I don't know what is the text going to be; so, I can't use character map for that.

Also it looks impossible or hard to code to replace the text; at least I need these characters:

My question is: How to print correctly?

Note: The font I'm using is this; However I tried using Tahoma too, but the problem still persists.

1 Answers

Seems strange, But magically Problem Solved!

I tried using another app to test called "Print2Pdf" and the text prints correctly, Maybe it's a bug of Microsoft XPS writer/reader that cannot print/read Arabic/Persian letters.

*Sorry for not providing link to Print2Pdf as I couldn't find the original website; If someone knows the original please add link; Thanks

Related