DrawRotatedText.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

Public Class DrawRotatedText
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.Pages.Add()
        Dim g = page.Graphics
        ' Set up some values to manage layout:
        Dim gap = g.Resolution
        Dim w = page.Size.Width / 3
        Dim h = w

        ' Draw text at different negative angles:
        Draw(g, New RectangleF(gap, gap, w, h), angle:=-90, False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=-60, False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=-45, False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=-30, False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        ' Draw text at different positive angles:
        page = doc.Pages.Add()
        g = page.Graphics
        Draw(g, New RectangleF(gap, gap, w, h), angle:=-90, False, RotatedTextAlignment.TopRight, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=-60, False, RotatedTextAlignment.TopRight, TextAlignment.Leading)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=-45, False, RotatedTextAlignment.TopRight, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=-30, False, RotatedTextAlignment.TopRight, TextAlignment.Leading)
        ' Draw text using different vertical stacking:
        page = doc.Pages.Add()
        g = page.Graphics
        Draw(g, New RectangleF(gap, gap, w, h), angle:=-30, verticalStacking:=True, RotatedTextAlignment.TopRight, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=-30, verticalStacking:=False, RotatedTextAlignment.TopRight, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=-30, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=-30, verticalStacking:=True, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        ' RotatedTextAlignment affects the location of text (red) within the target rectangle (green):
        page = doc.Pages.Add()
        g = page.Graphics
        Draw(g, New RectangleF(gap, gap, w, h), angle:=-30, verticalStacking:=True, RotatedTextAlignment.TopRight, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=60, verticalStacking:=False, RotatedTextAlignment.BottomRight, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=30, verticalStacking:=True, RotatedTextAlignment.TopLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=-60, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        ' Draw vertically stacked text using different TextAlignment values:
        page = doc.Pages.Add()
        g = page.Graphics
        Draw(g, New RectangleF(gap, gap, w, h), angle:=30, verticalStacking:=True, RotatedTextAlignment.TopLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=30, verticalStacking:=True, RotatedTextAlignment.TopLeft, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=30, verticalStacking:=True, RotatedTextAlignment.TopLeft, TextAlignment.Center)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=30, verticalStacking:=True, RotatedTextAlignment.TopLeft, TextAlignment.Distributed)
        ' Draw horizontally stacked text using different TextAlignment values:
        page = doc.Pages.Add()
        g = page.Graphics
        Draw(g, New RectangleF(gap, gap, w, h), angle:=-70, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Leading)
        Draw(g, New RectangleF(gap + w + gap, gap, w, h), angle:=-70, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Trailing)
        Draw(g, New RectangleF(gap, gap + h + gap, w, h), angle:=-70, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Center)
        Draw(g, New RectangleF(gap + w + gap, gap + h + gap, w, h), angle:=-70, verticalStacking:=False, RotatedTextAlignment.BottomLeft, TextAlignment.Distributed)

        doc.Save(stream)
        Return doc.Pages.Count
    End Function

    Shared Sub Draw(ByVal g As GcGraphics, ByVal rect As RectangleF, ByVal angle As Integer, ByVal verticalStacking As Boolean,
            ByVal rotatedAlign As RotatedTextAlignment, ByVal textAlign As TextAlignment)
        ' Draw a legend stating the current DrawRotatedText arguments' values:
        Dim tlLegend = g.CreateTextLayout()
        tlLegend.DefaultFormat.FontName = "Calibri"
        tlLegend.DefaultFormat.FontSize = 9
        tlLegend.AppendLine($"Rotation angle: {angle}°")
        tlLegend.AppendLine($"Text alignment: {textAlign}")
        tlLegend.AppendLine($"Rotated text alignment: {rotatedAlign}")
        tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}")
        g.DrawTextLayout(tlLegend, rect.Location)

        ' The target rectangle for the DrawRotatedText call:
        Dim d = tlLegend.ContentHeight + 12
        rect.Y += d
        rect.Height -= d

        ' Text layout to draw:
        Dim tl = g.CreateTextLayout()
        tl.DefaultFormat.FontName = "Calibri"
        tl.DefaultFormat.FontSize = 12
        tl.Append("The quick brown fox jumps over the lazy dog. ")
        tl.Append("The quick brown fox jumps over the lazy dog.")
        tl.TextAlignment = textAlign

        ' Outline the target rectangle in green:
        g.DrawRectangle(rect, New GCDRAW.Pen(Color.PaleGreen, 3))
        ' Outline the actual text rectangle in red:
        Dim tlCopy = tl.Clone(True)
        Dim tlRect = g.MeasureRotatedText(tlCopy, angle, verticalStacking, rect, rotatedAlign)
        g.DrawRectangle(tlRect, New GCDRAW.Pen(Color.Red, 1))

        ' Draw rotated text:
        g.DrawRotatedText(tl, angle, verticalStacking, rect, rotatedAlign)
    End Sub
End Class