ShapeFillFormats.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections
  8. Imports System.Collections.Generic
  9. Imports System.Linq
  10. Imports GrapeCity.Documents.Word
  11.  
  12. '' This sample demonstrates the various fill formats
  13. '' that can be used with shapes.
  14. Public Class ShapeFillFormats
  15. Function CreateDocx() As GcWordDocument
  16. Dim gradColors = New Color() {
  17. Color.Red,
  18. Color.Orange,
  19. Color.Yellow,
  20. Color.Green
  21. }
  22.  
  23. Dim patts As PatternFillType() = [Enum].GetValues(GetType(PatternFillType))
  24.  
  25. Dim doc = New GcWordDocument()
  26. Dim shapes = AddGeometryTypes(doc, New SizeF(100, 120), patts.Length + 12, True, True)
  27.  
  28. For Each s In shapes
  29. s.Size.Width.Value *= 2
  30. s.Size.Height.Value *= 2
  31. Next
  32.  
  33. doc.Body.Paragraphs.Insert("Fill formats", doc.Styles(BuiltInStyleId.Title), InsertLocation.Start)
  34.  
  35. Dim sIdx = 0
  36.  
  37. Dim shape = shapes(sIdx)
  38. sIdx += 1
  39.  
  40. shape.Fill.Type = FillType.Gradient
  41. shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomLeftToTopRight)
  42. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  43. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(3)
  44. shape.AddTextFrame("Linear grad")
  45.  
  46. shape = shapes(sIdx)
  47. sIdx += 1
  48. shape.Fill.Type = FillType.Gradient
  49. shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomRightToTopLeft)
  50. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  51. shape.Fill.GradientFill.Stops(1).Position = 30
  52. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(1)
  53. shape.Fill.GradientFill.Stops.Add(gradColors(2), 60)
  54. shape.Fill.GradientFill.Stops.Add(gradColors(3), 90)
  55. shape.AddTextFrame("Linear grad")
  56.  
  57. shape = shapes(sIdx)
  58. sIdx += 1
  59. shape.Fill.Type = FillType.Gradient
  60. shape.Fill.GradientFill.Type = GradientType.Circle '' not really needed if using ApplyPathDirection() method
  61. shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.FromBottomRight)
  62. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  63. shape.Fill.GradientFill.Stops(1).Position = 30
  64. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(1)
  65. shape.Fill.GradientFill.Stops.Add(gradColors(2), 60)
  66. shape.Fill.GradientFill.Stops.Add(gradColors(3), 90)
  67. shape.AddTextFrame("Circle grad")
  68.  
  69. shape = shapes(sIdx)
  70. sIdx += 1
  71. shape.Fill.Type = FillType.Gradient
  72. shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.Center)
  73. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  74. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(3)
  75. shape.AddTextFrame("Circle grad")
  76.  
  77. shape = shapes(sIdx)
  78. sIdx += 1
  79. shape.Fill.Type = FillType.Gradient
  80. shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.Center)
  81. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  82. shape.Fill.GradientFill.Stops(1).Position = 30
  83. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(1)
  84. shape.Fill.GradientFill.Stops.Add(gradColors(2), 60)
  85. shape.Fill.GradientFill.Stops.Add(gradColors(3), 90)
  86. shape.AddTextFrame("Rectangle grad")
  87.  
  88. shape = shapes(sIdx)
  89. sIdx += 1
  90. shape.Fill.Type = FillType.Gradient
  91. shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.FromTopRight)
  92. shape.Fill.GradientFill.Stops(0).Color.RGB = gradColors(0)
  93. shape.Fill.GradientFill.Stops(1).Color.RGB = gradColors(3)
  94. shape.AddTextFrame("Rectangle grad")
  95.  
  96. '' Image fills:
  97. shape = shapes(sIdx)
  98. sIdx += 1
  99. shape.Fill.Type = FillType.Image
  100. Dim bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "butterfly.jpg"))
  101. shape.Fill.ImageFill.SetImage(bytes, "image/jpeg")
  102. shape = shapes(sIdx)
  103. sIdx += 1
  104. shape.Fill.Type = FillType.Image
  105. shape.Fill.ImageFill.FillType = ImageFillType.Tile
  106. ''use tile image as flipped both vertical and horizontal
  107. shape.Fill.ImageFill.Tile.Flip = TileFlipMode.HorizontalAndVertical
  108. ''scale image down horizontally to 30% and vertically to 25%
  109. shape.Fill.ImageFill.Tile.HorizontalScale = 30
  110. shape.Fill.ImageFill.Tile.VerticalScale = 25
  111. bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "gcd-hex-logo-80x80.png"))
  112. shape.Fill.ImageFill.SetImage(bytes, "image/png")
  113.  
  114. Dim r = shape.GetRange().Runs.Insert($"{vbCrLf}{vbCrLf}Pattern fills:{vbCrLf}", doc.Styles(BuiltInStyleId.Strong), InsertLocation.After)
  115. r.Font.Size *= 3
  116.  
  117. '' Pattern fills:
  118. For Each p In patts
  119. shape = shapes(sIdx)
  120. sIdx += 1
  121. shape.Size.Width.Value /= 3
  122. shape.Size.Height.Value /= 3
  123. shape.Fill.Type = FillType.Pattern
  124. ''type represents how it will look
  125. shape.Fill.PatternFill.Type = CType(p, PatternFillType)
  126. shape.Fill.PatternFill.ForeColor.RGB = gradColors(0)
  127. shape.Fill.PatternFill.BackColor.RGB = gradColors(gradColors.Length - 1)
  128. Next
  129.  
  130.  
  131. If (sIdx < shapes.Count) Then
  132. shapes.Skip(sIdx).ToList().ForEach(Sub(s_) s_.Delete())
  133. End If
  134.  
  135. Return doc
  136. End Function
  137.  
  138. ''' <summary>
  139. ''' Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  140. ''' The fill and line colors of the shapes are varied.
  141. ''' </summary>
  142. ''' <param name="doc">The target document.</param>
  143. ''' <param name="size">The size of shapes to create.</param>
  144. ''' <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  145. ''' <param name="skipUnfillable">Add only shapes that support fills.</param>
  146. ''' <param name="noNames">Do not add geometry names as shape text frames.</param>
  147. ''' <returns>The list of shapes added to the document.</returns>
  148. Private Shared Function AddGeometryTypes(doc As GcWordDocument, size As SizeF, Optional count As Integer = -1, Optional skipUnfillable As Boolean = False, Optional noNames As Boolean = False) As List(Of Shape)
  149.  
  150. '' Line and fill colors:
  151. Dim lines = New Color() {Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue}
  152. Dim line = 0
  153. Dim fills = New Color() {Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed}
  154. Dim fill = 0
  155.  
  156. '' The supported geometry types:
  157. Dim geoms As GeometryType() = [Enum].GetValues(GetType(GeometryType))
  158.  
  159. '' Add a paragraph and a run where the shapes will live:
  160. doc.Body.Paragraphs.Add("")
  161.  
  162. Dim shapes = New List(Of Shape)
  163. For Each g In geoms
  164. '' Line geometries do not support fills:
  165. If skipUnfillable AndAlso g.IsLineGeometry() Then
  166. Continue For
  167. End If
  168. If count = 0 Then
  169. Exit For
  170. End If
  171. count -= 1
  172.  
  173. Dim w = size.Width, h = size.Height
  174. Dim shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g)
  175. If Not g.IsLineGeometry() Then
  176. shape.Fill.Type = FillType.Solid
  177. If fill < fills.Length - 1 Then
  178. fill += 1
  179. Else
  180. fill = 0
  181. End If
  182. shape.Fill.SolidFill.RGB = fills(fill)
  183. End If
  184. shape.Line.Width = 3
  185. If line < lines.Length - 1 Then
  186. line += 1
  187. Else
  188. line = 0
  189. End If
  190. shape.Line.Fill.SolidFill.RGB = lines(line)
  191. If Not noNames AndAlso g.TextFrameSupported() Then
  192. shape.AddTextFrame(g.ToString())
  193. End If
  194. shape.AlternativeText = $"This is shape {g}"
  195. shape.Size.EffectExtent.AllEdges = 8
  196. shapes.Add(shape)
  197. Next
  198. Return shapes
  199. End Function
  200. End Class
  201.