Search code examples
c#winformsprintingsystem.drawing

System.Drawing.Printing Drawing String vertically fromRight to Left in rectangle center alignment


I want to draw a string in a specified rectangle(vertical direction), Below code gives me wat I am after but the text flow is from Left to Right, What I am trying to is Right to Left. Like Line 1 on the right side and Line 2 is on left side. I did Transformation also, but no luck.

 RectangleF tabbor = new RectangleF(0, 0,borHeight, 44.35F);
        StringFormat sf = new StringFormat();
        //if (cmbDir.SelectedItem.Equals("Vertical"))
        //    sf.FormatFlags = StringFormatFlags.DirectionVertical;
        sf.LineAlignment = StringAlignment.Center;
        sf.Alignment = StringAlignment.Center;
        SolidBrush drawBrush = new SolidBrush(Color.Black);
        //Do 180 degree Rotatation Transformation
        ev.Graphics.RotateTransform(90, MatrixOrder.Append);                   
        ev.Graphics.TranslateTransform(xPos+44.35F, yPos, MatrixOrder.Append);                    
        ev.Graphics.DrawString("T", printFont, Brushes.Black, tabbor, sf);
        if (cbPreview.Checked)
            ev.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(tabbor));

enter image description here I am looking for the text from top to bottom(now its in reverse), line position right to left(this is working)

enter image description here


Solution

  • You can achieve this by doing a 180 degree rotation transformation.Check the below code

            RectangleF tabbor = new RectangleF(0, 0, 44.35F,150.0F);         
            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.DirectionVertical;            
            String drawString = "First Line Second Line";
            Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            float x = 0F;
            float y = 0F;
            StringFormat drawFormat = new StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            //Do 180 degree Rotatation Transformation
            ev.Graphics.RotateTransform(180,MatrixOrder.Append);
            ev.Graphics.TranslateTransform(50, 150,MatrixOrder.Append);
            ev.Graphics.DrawString(drawString, drawFont, Brushes.Black, tabbor,sf);               
            ev.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(tabbor));