4

I'm trying to send khmer script(unicode) string to printer using PrintDocument provided by the .NET framework.

Unfortunately it seems to me that the Graphics.DrawString() does not render khmer script correctly.

Platform: Windows 7 Ultimate
IDE: VS 2010 Ultimate

Here is the sample code:

void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
  var font = new Font("Khmer UI", 12);
  var text = "សួស្តី"; // "Hello"
  e.Graphics.DrawString(text, font, Brushes.Black, 100, 100);
}
lchanmann
  • 357
  • 2
  • 10

2 Answers2

5

mann,
I tested your code on a Form_Paint() handler, and I got exactly what you said.
But when I used this instead:

TextRenderer.DrawText(e.Graphics, text, font, new Point(100, 100), Color.Black);  

It gave me the text the way you wanted it.
Try that on your printDoc_PrintPage().

alt text

BeemerGuy
  • 8,139
  • 2
  • 35
  • 46
  • That because Graphics class use GDI+ and TextRenderer class use GDI to do the rendering. Unfortunately TextRenderer class does not support for printing. http://msdn.microsoft.com/en-us/library/ms171753.aspx – lchanmann Nov 09 '10 at 02:11
  • Hi sir, I accept your recommend. But when I try your code, I am very difficult to set location in New Point(100,100). It means that can I set New Point as Milimeter of Document. Let say, I want to print on A4 paper. How can I print to the point that I want. – Sopolin Aug 30 '11 at 03:34
  • @Sopolin -- you'll have to calculate that. You need to convert millimeters into pixels. By default, there are 72 pixels per inch. There are 25.4mm per inch, which means the resolution is 72 pixels per 25.4mm. Therefore, for example, if you want to start drawing at 50mm, then you need to draw at pixel 142, as calculated by 50 * (72/25.4). – BeemerGuy Aug 30 '11 at 08:05
  • Thank you sir, but I still can't because I use Paper A4 to print text out. It shows different location, even though I print what you have guide me. Let say, I print (x,y) in milimeter (17,35) convert to pixels (x,y) (48,99). It doesn't print text to location on paper that I want. – Sopolin Aug 30 '11 at 08:39
  • @Sopolin -- Are you factoring in the margins of the paper? Make sure you don't have any margin settings there that shift your numbers over. And the question is, where does the (17,35) show? Too far to the bottom? Too far to the right? How much off is it? – BeemerGuy Aug 30 '11 at 17:59
  • @BeemerGuy thanks for your answer, it also helped me to support unicode emojis. – Fabian Köbel Jun 27 '16 at 14:33
  • I have the same problem with Unicode chars, but I need to write the text to a bitmap and it seems that TextRenderer.Draw() has some problems there. Any idea how can I handle this? More details here: https://stackoverflow.com/questions/71694798/how-to-convert-unicode-text-into-a-bitmap – ilCosmico Apr 01 '22 at 13:19
1

Thanks Albin and Beemer for your active response.

After a few posts in c# google group. It's been confirmed that there is a bug in GDI+ that incorrectly show certain script ("Khmer" in this case) to a different wording.

A native win32 test application was created to verify the issue with GDI+ DrawString().

A bug report has been submitted to Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/details/620081/

lchanmann
  • 357
  • 2
  • 10