SlantedTableHeaders.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 System.Numerics
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

Public Class SlantedTableHeaders
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.Pages.Add()
        Dim g = page.Graphics

        ' This is just to better position the table in the page:
        g.Transform = Matrix3x2.CreateTranslation(g.Resolution, g.Resolution)

        Dim x = New Single() {10, 70, 120, 170, 220, 270, 320, 370}
        Dim y = New Single() {10, 70, 90, 110, 130}
        Dim lastX As Integer = x.Length - 1
        Dim lastY As Integer = y.Length - 1

        Dim angle As Integer = -50
        Dim dx = CSng((y(0) - y(1)) / Math.Tan(Math.PI * angle / 180))

        Using path = g.CreatePath()
            path.BeginFigure(New PointF(x(1) + dx, y(0)))
            path.AddLine(New PointF(x(lastX) + dx, y(0)))
            path.AddLine(New PointF(x(lastX), y(1)))
            path.AddLine(New PointF(x(1), y(1)))
            path.EndFigure(FigureEnd.Closed)
            g.FillPath(path, Color.Bisque)
        End Using
        DrawLine(g, x(1) + dx, y(0), x(lastX) + dx, y(0))
        For i = 1 To lastX
            DrawLine(g, x(i), y(1), x(i) + dx, y(0))
        Next

        Dim arrH = New String() {"January", "February", "March", "April", "May", "June"}
        For i = 1 To lastX - 1
            DrawSlantedText(g, angle, x(i), y(0), x(i + 1), y(1), arrH(i - 1))
        Next

        For i = 1 To lastY
            DrawLine(g, x(0), y(i), x(lastX), y(i))
        Next
        For i = 0 To lastX
            DrawLine(g, x(i), y(1), x(i), y(lastY))
        Next

        Dim arr1 = New String() {"Store 1", "83424", "13558", "2348", "6429", "7565", "2833"}
        Dim arr2 = New String() {"Store 2", "53423", "3573", "32321", "16474", "97553", "8364"}
        Dim arr3 = New String() {"Store 3", "3421", "83553", "2343", "6428", "77557", "8347"}

        Dim bold As Boolean = True
        For i = 0 To lastX - 1
            DrawText(g, y(1), x(i), x(i + 1), arr1(i), bold)
            DrawText(g, y(2), x(i), x(i + 1), arr2(i), bold)
            DrawText(g, y(3), x(i), x(i + 1), arr3(i), bold)
            bold = False
        Next

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

    Shared Sub DrawLine(ByVal g As GcGraphics, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single)
        g.DrawLine(New PointF(x1, y1), New PointF(x2, y2), New GCDRAW.Pen(Color.Gray, 1))
    End Sub

    Shared Sub DrawSlantedText(ByVal g As GcGraphics, ByVal angle As Integer, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal s As String)
        Dim tl = g.CreateTextLayout()
        tl.TextAlignment = TextAlignment.Center
        tl.Append(s, New TextFormat With {
            .FontName = "Segoe UI",
            .FontSize = 10,
            .FontBold = True
        })
        Dim rc = New RectangleF(x1, y1, x2 - x1, y2 - y1)
        g.DrawSlantedText(tl, angle, False, rc, SlantedTextAlignment.CenterInsideOutside)
    End Sub

    Shared Sub DrawText(ByVal g As GcGraphics, ByVal y1 As Single, ByVal x1 As Single, ByVal x2 As Single, ByVal s As String, Optional ByVal bold As Boolean = False)
        Dim tl = g.CreateTextLayout()
        tl.TextAlignment = If(bold, TextAlignment.Leading, TextAlignment.Trailing)
        tl.MaxWidth = x2 - x1 - 6
        tl.Append(s, New TextFormat With {
            .FontName = "Segoe UI",
            .FontSize = 10,
            .FontBold = bold
        })
        g.DrawTextLayout(tl, New PointF(x1 + 3, y1))
    End Sub
End Class