GroupShapes.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 use of group shapes.
  13. '' Group shapes allow to group several shapes,
  14. '' and apply the same settings (such as fills)
  15. '' to all grouped shapes at once.
  16. Public Class GroupShapes
  17. Function CreateDocx() As GcWordDocument
  18. '' Layout constants:
  19. Const N = 12
  20. Const NCOLS = 3
  21. Const NROWS = 4
  22. Const SIDEX = 130
  23. Const SIDEY = 120
  24. Const DX = 6
  25. Const DY = 4
  26.  
  27. Dim doc = New GcWordDocument()
  28.  
  29. '' Add a few ungrouped shapes to the document:
  30. Dim shapes = AddGeometryTypes(doc, New SizeF(SIDEX, SIDEY), N, True, False)
  31.  
  32. '' Spread out the shapes:
  33. For i = 0 To NROWS - 1
  34. For j = 0 To NCOLS - 1
  35. Dim k = i * NCOLS + j
  36. If (k >= N) Then
  37. Exit For
  38. End If
  39. shapes(k).Position.Horizontal.Offset = (SIDEX + DX) * j
  40. shapes(k).Position.Vertical.Offset = (SIDEY + DY) * i
  41. Next
  42. Next
  43.  
  44. '' Add the title:
  45. doc.Body.Paragraphs.Insert("Group shape with an image fill", doc.Styles(BuiltInStyleId.Title), InsertLocation.Start)
  46.  
  47. '' Add the group containing all added shapes:
  48. Dim group As GroupShape
  49. Using shapesRange = doc.Body.GetPersistentRange(shapes(0).Start, shapes.Last().End)
  50. group = shapesRange.GroupShapes.Insert(RangeLocation.Whole)
  51. End Using
  52.  
  53. '' Adjust the group's size:
  54. group.Size.Width.Value = SIDEX * NCOLS + DX * (NCOLS - 1)
  55. group.Size.Height.Value = SIDEY * NROWS + DY * (NROWS - 1)
  56.  
  57. '' Create an image fill on the group shape:
  58. group.Fill.Type = FillType.Image
  59. Dim bytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "roofs.jpg"))
  60. group.Fill.ImageFill.SetImage(bytes, "image/jpeg")
  61. '' And apply it to the group:
  62. group.ApplyGroupFill()
  63.  
  64. Return doc
  65. End Function
  66.  
  67. ''' <summary>
  68. ''' Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  69. ''' The fill and line colors of the shapes are varied.
  70. ''' </summary>
  71. ''' <param name="doc">The target document.</param>
  72. ''' <param name="size">The size of shapes to create.</param>
  73. ''' <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  74. ''' <param name="skipUnfillable">Add only shapes that support fills.</param>
  75. ''' <param name="noNames">Do not add geometry names as shape text frames.</param>
  76. ''' <returns>The list of shapes added to the document.</returns>
  77. 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)
  78.  
  79. '' Line and fill colors:
  80. Dim lines = New Color() {Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue}
  81. Dim line = 0
  82. Dim fills = New Color() {Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed}
  83. Dim fill = 0
  84.  
  85. '' The supported geometry types:
  86. Dim geoms As GeometryType() = [Enum].GetValues(GetType(GeometryType))
  87.  
  88. '' Add a paragraph and a run where the shapes will live:
  89. doc.Body.Paragraphs.Add("")
  90. Dim run = doc.Body.Runs.Last
  91.  
  92. Dim shapes = New List(Of Shape)
  93. For Each g In geoms
  94. '' Line geometries do not support fills:
  95. If skipUnfillable AndAlso g.IsLineGeometry() Then
  96. Continue For
  97. End If
  98. If count = 0 Then
  99. Exit For
  100. End If
  101. count -= 1
  102.  
  103. Dim w = size.Width, h = size.Height
  104. Dim shape = run.GetRange().Shapes.Add(w, h, g)
  105. If Not g.IsLineGeometry() Then
  106. shape.Fill.Type = FillType.Solid
  107. If fill < fills.Length - 1 Then
  108. fill += 1
  109. Else
  110. fill = 0
  111. End If
  112. shape.Fill.SolidFill.RGB = fills(fill)
  113. End If
  114. shape.Line.Width = 3
  115. If line < lines.Length - 1 Then
  116. line += 1
  117. Else
  118. line = 0
  119. End If
  120. shape.Line.Fill.SolidFill.RGB = lines(line)
  121. If Not noNames AndAlso g.TextFrameSupported() Then
  122. shape.AddTextFrame(g.ToString())
  123. End If
  124. shape.AlternativeText = $"This is shape {g}"
  125. shape.Size.EffectExtent.AllEdges = 8
  126. shapes.Add(shape)
  127. Next
  128. Return shapes
  129. End Function
  130. End Class
  131.