Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have PrintPageEvent to print document in C# and I want to print text with align right. When the text starts with character, it prints normal. But if the text starts with number, data after printing is wrong.

For example:
Text to print: "Print a text" => Result: "Print a text"
Text to print: "10 test 8" => Result: "test 8 10"

What's wrong? Does anyone know it.

What I have tried:

I use this code line to print string
C#
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString("10 test 8", new Font("Arial", 5), new SolidBrush(Color.Black), 10, 5, stringFormat);

If I remove StringFormat from DrawString function (print left to right), the printing is right.
Posted
Updated 28-Feb-23 23:54pm
v2

1 solution

StringFormatFlags.DirectionRightToLeft is for languages like Arabic which are written right-to-left.
StringFormatFlags Enum (System.Drawing) | Microsoft Learn[^]

If you just want to right-align a string of left-to-right text, then you need to specify the containing rectangle, and set the StringFormat's Alignment property to StringAlignment.Far.
StringAlignment Enum (System.Drawing) | Microsoft Learn[^]

c# - Right-Aligning printed text - StackOverflow[^]
 
Share this answer
 
Comments
huynhminh97 1-Mar-23 21:35pm    
It's so great, I did it. Thanks you so much @Richard Deeming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900