Printing Right-To-Left in c#

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.

Answers


Use StringFormatFlags.DirectionRightToLeft, like this:

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

You could make a very simple coordinate transformation:

public static class CoordinateConverter
{
    public static RectangleF Convert(RectangleF source, RectangleF drawArea)
    {
        // I assume drawArea.X to be 0
        return new RectangleF(
            drawArea.Width - source.X - source.Width,
            source.Y,
            source.Width,
            source.Height);
    }

    public static RectangleF ConvertBack(Rectangle source, RectangleF drawArea)
    {
       return new RectangleF(
           source.X + source.Width - drawArea.Width,
           source.Y,
           source.Width,
           source.Height);
    }
}

Now every time you want something texty to be drawn, you can use this converter to change the coordinates. Of course you could also ref a rectangle so you don't create new ones all the time. But the principle stays the same. I hope I understood your question correctly.


Need Your Help

Does the attr() in jQuery force lowercase?

javascript jquery svg attr lowercase

I'm trying to manipulate the svg 'viewBox' attribute which looks something like this: