Reports for WinForms | ComponentOne
In This Topic
    Drawing Text at an Angle
    In This Topic

    To draw text at an angle, use the Graphics object and create a subroutine to rotate text.

    1. Navigate to the Toolbox and add the C1PrintPreviewControl and C1PrintDocument controls to your project.
    2. Click C1PrintPreviewControl1 to select it and in the Properties window set its Document property to C1PrintDocument1.
    3. Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1PrintDocument1.StartDoc()    
      Me.C1PrintDocument1.RenderBlockGraphicsBegin()
      
      ' Declare the graphics object.    
      Dim g As System.Drawing.Graphics    
      g = Me.C1PrintDocument1.CurrentBlockGraphics   
      
      Dim fontb = New Font("Arial", 32, FontStyle.Bold)
      
      ' Subroutine to alter text angle.    
      RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, -150, 150)
      
      Me.C1PrintDocument1.RenderBlockGraphicsEnd()    
      Me.C1PrintDocument1.EndDoc()
      

      To write code in C#

      C#
      Copy Code
      this.c1PrintDocument1.StartDoc();    
      this.c1PrintDocument1.RenderBlockGraphicsBegin(); 
       
      // Declare the graphics object.    
      System.Drawing.Graphics g;    
      g = this.c1PrintDocument1.CurrentBlockGraphics; 
      
      Font fontb = new Font("Arial", 12, FontStyle.Bold);
      
      // Subroutine to alter text angle.    
      RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, 10, 100);
      
      this.c1PrintDocument1.RenderBlockGraphicsEnd();    
      this.c1PrintDocument1.EndDoc();
      
    4. Add the following RotateText subroutine, which draws the text at an angle:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Public Sub RotateText(ByVal g As Graphics, ByVal f As Font, ByVal s As String, ByVal angle As Single, ByVal b As Brush, ByVal x As Single, ByVal y As Single)    
          If angle > 360 Then    
              While angle > 360    
                  angle = angle - 360    
              End While    
          ElseIf angle < 0 Then    
              While angle < 0    
                  angle = angle + 360    
              End While    
          End If
      
          ' Create a matrix and rotate it n degrees.    
          Dim myMatrix As New System.Drawing.Drawing2D.Matrix    
          myMatrix.Rotate(angle, Drawing2D.MatrixOrder.Append)
      
          ' Draw the text to the screen after applying the transform.    
          g.Transform = myMatrix    
          g.DrawString(s, f, b, x, y)    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      public void RotateText(Graphics g, Font f, string s, Single angle, Brush b, Single x, Single y)    
      {    
          if (angle > 360)    
          {    
              while (angle > 360)    
              {    
                  angle = angle - 360;    
              }    
          }    
          else if (angle < 0)    
          {    
              while (angle < 0)    
              {    
                  angle = angle + 360;    
              }    
          }
      
          // Create a matrix and rotate it n degrees.    
          System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix();    
          myMatrix.Rotate(angle, System.Drawing.Drawing2D.MatrixOrder.Append);
      
          // Draw the text to the screen after applying the transform.    
          g.Transform = myMatrix;    
          g.DrawString(s, f, b, x, y);    
      }
      

    What You've Accomplished

    The text you added appears at a 45 degree angle: