Gradients.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
  14. Public Class Gradients
  15. Function CreatePDF(ByVal stream As Stream) As Integer
  16. Dim doc = New GcPdfDocument()
  17. Dim g = doc.NewPage().Graphics
  18. Dim testRectSize = New SizeF(72 * 4, 72)
  19. Dim dy = 12
  20. '' TextLayout to draw labels:
  21. Dim tl = g.CreateTextLayout()
  22. tl.DefaultFormat.Font = StandardFonts.Times
  23. tl.DefaultFormat.FontSize = 18
  24. tl.DefaultFormat.ForeColor = Color.Chartreuse
  25. tl.MaxWidth = testRectSize.Width
  26. tl.MaxHeight = testRectSize.Height
  27. tl.TextAlignment = TextAlignment.Center
  28. tl.ParagraphAlignment = ParagraphAlignment.Center
  29. '' Note 1:
  30. Dim rc = Util.AddNote("Linear gradients using LinearGradientBrush:", doc.Pages.Last, New RectangleF(72, 36, 500, 100))
  31. '' Text insertion point:
  32. Dim ip = New PointF(rc.Left, rc.Bottom + dy)
  33. '' Local action to draw a gradient-filled rectangle:
  34. Dim drawSwatch As Action(Of GCDRAW.Brush, String) =
  35. Sub(ByVal b_, ByVal txt_)
  36. Dim rect = New RectangleF(ip, testRectSize)
  37. '' Fill the rectangle with a gradient brush:
  38. g.FillRectangle(rect, b_)
  39. '' Draw a border, text info etc:
  40. g.DrawRectangle(rect, Color.Magenta)
  41. tl.Clear()
  42. tl.Append(txt_)
  43. tl.MaxHeight = testRectSize.Height
  44. tl.MaxWidth = testRectSize.Width
  45. tl.PerformLayout(True)
  46. g.DrawTextLayout(tl, ip)
  47. ip.Y += rect.Height + dy
  48. End Sub
  49. '' LinearGradientBrush:
  50. '' Horizontal gradient:
  51. Dim linearGradBrush = New LinearGradientBrush(Color.Red, Color.Blue)
  52. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  53. '' Vertical gradient:
  54. linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Green, New PointF(0, 1))
  55. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  56. '' Diagonal gradient (increase swatch height to better show diagonal):
  57. testRectSize.Height *= 2
  58. linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Teal, New PointF(1, 1))
  59. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  60. '' RadialGradientBrush
  61. rc = Util.AddNote("Radial gradients using RadialGradientBrush:", doc.Pages.Last, New RectangleF(ip, New SizeF(500, 100)))
  62. ip.Y = rc.Bottom + dy
  63. '' Centered:
  64. Dim radialGradBrush = New RadialGradientBrush(Color.Orange, Color.Purple)
  65. drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
  66. '' Center in bottom right corner:
  67. radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
  68. drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
  69. ''
  70. '' Done:
  71. doc.Save(stream)
  72. Return doc.Pages.Count
  73. End Function
  74. End Class
  75.