Gradients.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
Public Class Gradients
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim g = doc.NewPage().Graphics
        Dim testRectSize = New SizeF(72 * 4, 72)
        Dim dy = 12
        '' TextLayout to draw labels:
        Dim tl = New TextLayout()
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 18
        tl.DefaultFormat.ForeColor = Color.Chartreuse
        tl.MaxWidth = testRectSize.Width
        tl.MaxHeight = testRectSize.Height
        tl.TextAlignment = TextAlignment.Center
        tl.ParagraphAlignment = ParagraphAlignment.Center
        '' Note 1:
        Dim rc = Util.AddNote("Linear gradients using LinearGradientBrush:", doc.Pages.Last, New RectangleF(72, 36, 500, 100))
        '' Text insertion point:
        Dim ip = New PointF(rc.Left, rc.Bottom + dy)
        '' Local action to draw a gradient-filled rectangle:
        Dim drawSwatch As Action(Of Brush, String) =
            Sub(ByVal b_, ByVal txt_)
                Dim rect = New RectangleF(ip, testRectSize)
                '' Fill the rectangle with a gradient brush:
                g.FillRectangle(rect, b_)
                '' Draw a border, text info etc:
                g.DrawRectangle(rect, Color.Magenta)
                tl.Clear()
                tl.Append(txt_)
                tl.MaxHeight = testRectSize.Height
                tl.MaxWidth = testRectSize.Width
                tl.PerformLayout(True)
                g.DrawTextLayout(tl, ip)
                ip.Y += rect.Height + dy
            End Sub
        '' LinearGradientBrush:
        '' Horizontal gradient:
        Dim linearGradBrush = New LinearGradientBrush(Color.Red, Color.Blue)
        drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
        '' Vertical gradient:
        linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Green, New PointF(0, 1))
        drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
        '' Diagonal gradient (increase swatch height to better show diagonal):
        testRectSize.Height *= 2
        linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Teal, New PointF(1, 1))
        drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
        '' RadialGradientBrush
        rc = Util.AddNote("Radial gradients using RadialGradientBrush:", doc.Pages.Last, New RectangleF(ip, New SizeF(500, 100)))
        ip.Y = rc.Bottom + dy
        '' Centered:
        Dim radialGradBrush = New RadialGradientBrush(Color.Orange, Color.Purple)
        drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
        '' Center in bottom right corner:
        radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
        drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class