C# – Printing Right-To-Left in c#

cgraphicsnetprinting

According to msdn: http://www.microsoft.com/middleeast/msdn/arabicsupp.aspx

How GDI+ Support Arabic?

GDI+ supports Arabic text manipulation including print text with RTL reading order for both output devices, Screen and Printer. The Graphics.DrawString method draws the specified text string at a designated x, y location or rectangle (according to its overloading), with the specified Brush and Font objects using the formatting attributes of the specified StringFormat object. The StringFormat object includes text layout information such as text reading order.

Therefore, you can easily move the origin of the graphics object to be Right-Top, instead of Left-Top, to print out the Arabic text in the designated location on the screen smoothly, without having to calculate locations explicit.

While this is true when setting (X,Y) coordination to (0,0) but if I want to increase X coordination to print at specific area on the paper, the X coordination will increase to the right side of the paper not to the left as it supposed to when printing in Right-to-left; which means print outside of the paper.
See this demo:

static void Main(string[] args)
{
    PrintDocument p = new PrintDocument();
    p.PrintPage += new PrintPageEventHandler(PrintPage);
    p.Print();
}

static void PrintPage(object sender, PrintPageEventArgs e)
{
    string drawString = "إختبار الرسم";
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    Font drawFont = new System.Drawing.Font("Arail", 16);
    RectangleF recAtZero = new RectangleF(0, 0, e.PageBounds.Width, e.PageBounds.Height);
    StringFormat drawFormat = new StringFormat();

    drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;

    e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero, drawFormat);
    RectangleF recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height);
    e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
}

How to move the origin of the graphics object to be Right-Top instead of Left-Top and when increase X coordination it advance the printing point to the left not to the right.

PS: What I am doing now is setting X coordination to negative to force it move to left.
Simple digagram

Best Answer

Use StringFormatFlags.DirectionRightToLeft, like this:

StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
e.Graphics.DrawString("سلام",
                this.Font,
                new SolidBrush(Color.Red),
                r1,
                format);
Related Topic