2

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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
S0m3on
  • 21
  • 3
  • Does this answer your question? [Encoding Persian string using C#](https://stackoverflow.com/questions/62621119/encoding-persian-string-using-c-sharp) – demo Apr 05 '21 at 13:03
  • It's problem is with encoding, I have problem with direction – S0m3on Apr 05 '21 at 13:05
  • https://stackoverflow.com/questions/25219654/c-sharp-print-right-to-left#comment39281362_25219654? – GSerg Apr 05 '21 at 13:18
  • nope i tried searching – S0m3on Apr 05 '21 at 13:20
  • The problem is that I use exactly the same code but It prints reverse – S0m3on Apr 05 '21 at 13:22
  • Maybe using web browser and javascript can be a workaround? But it isn't a solution – S0m3on Apr 05 '21 at 13:28
  • Did you actually read the comment? Have you tried specifying a rectangle and adding `\u200f` as suggested? – GSerg Apr 05 '21 at 13:35
  • 1
    What print driver are you using? Is it s PCL or PS driver? Make sure you are using a PS driver since you are using PS fonts. – jdweng Apr 05 '21 at 13:48
  • GSerg Sorry for misunderstanding but his problem is that the anchors are in the top-right not top-left btw I'll try it and I'll report that – S0m3on Apr 05 '21 at 14:04
  • jdweng I don't know what they are; I just used Microsoft XPS for testing – S0m3on Apr 05 '21 at 14:06
  • GSerg Adding \u200f didn't work; Maybe It's a PCL driver (as jdweng mentioned) and that caused the issue – S0m3on Apr 05 '21 at 14:57

1 Answers1

0

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

S0m3on
  • 21
  • 3