在C#中打印从右到左右到左

2023-09-02 10:51:16 作者:孤独是场无人送药的重感冒

根据MSDN:http://www.microsoft.com/middleeast/msdn/arabicsupp.aspx

如何GDI +支持阿拉伯语?

How GDI+ Support Arabic?

GDI +支持阿拉伯文文本操作包括两个输出设备,屏幕和打印机RTL阅读顺序打印的文本。该Graphics.DrawString方法绘制指定的文本字符串在指定的x,y位置或矩形(根据其超载),用指定的画笔,并使用指定的StringFormat对象的格式属性Font对象。该的StringFormat对象包括文本布局信息,如文本阅读顺序。

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.

虽然设定(X,Y)的协调,以(0,0),但如果欲增加X11协调在特定区域上的纸打印时,X协调将增加至右侧的纸的时候,这是真不是向左,因为它应该从右到左打印时;这意味着打印纸的外部。 看到这个演示:

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);
}

如何移动原点的图形对象是右键顶代替左顶和当增加X11协调它在印刷点前进到左侧不到右侧。

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:我在做什么现在x设置的协调负,迫使其移动到左。

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

推荐答案

使用StringFormatFlags.DirectionRightToLeft,像这样的:

Use StringFormatFlags.DirectionRightToLeft, like this:

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