ThemedShapeStyles.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 available predefined themed shape styles
  13. '' that are supported by DsWord.
  14. '' We first generate a number of different shapes with varying fill
  15. '' and line colors, then duplicate that shape, and apply a themed style
  16. '' to the copy.
  17. Public Class ThemedShapeStyles
  18. Function CreateDocx() As GcWordDocument
  19.  
  20. Dim styles = GetType(ThemedShapeStyle).GetFields(System.Reflection.BindingFlags.Public Or System.Reflection.BindingFlags.Static).Where(Function(p_) p_.FieldType = GetType(ThemedShapeStyle))
  21. Dim stylesCount = styles.Count()
  22. Dim doc = New GcWordDocument()
  23.  
  24. '' We will apply each preset to 2 consecutive shapes:
  25. Dim shapes = AddGeometryTypes(doc, New SizeF(100, 100), stylesCount * 2, True, True)
  26.  
  27. doc.Body.Paragraphs.Insert($"Themed Shape Styles ({stylesCount})", doc.Styles(BuiltInStyleId.Title), InsertLocation.Start)
  28.  
  29. If (shapes.Count() > stylesCount * 2) Then
  30. shapes.Skip(stylesCount).ToList().ForEach(Sub(s_) s_.Delete())
  31. End If
  32.  
  33. Dim styleIdx = 0
  34. Dim flop = 0
  35. For Each s In shapes
  36. Dim shape As Shape = s.GetRange().CopyTo(s.GetRange(), InsertLocation.After).ParentObject
  37.  
  38. Dim style = styles.ElementAt(styleIdx)
  39. If (flop Mod 2) <> 0 Then
  40. styleIdx += 1
  41. End If
  42. flop += 1
  43. '' Apply the themed style to the shape:
  44. shape.ApplyThemedStyle(CType(style.GetValue(Nothing), ThemedShapeStyle))
  45. '' Insert the style's name in front of the styled shape:
  46. shape.GetRange().Runs.Insert($"{style.Name}:", InsertLocation.Before)
  47. shape.GetRange().Runs.Insert(vbCrLf, InsertLocation.After)
  48. Next
  49.  
  50. Return doc
  51. End Function
  52.  
  53. ''' <summary>
  54. ''' Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  55. ''' The fill and line colors of the shapes are varied.
  56. ''' </summary>
  57. ''' <param name="doc">The target document.</param>
  58. ''' <param name="size">The size of shapes to create.</param>
  59. ''' <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  60. ''' <param name="skipUnfillable">Add only shapes that support fills.</param>
  61. ''' <param name="noNames">Do not add geometry names as shape text frames.</param>
  62. ''' <returns>The list of shapes added to the document.</returns>
  63. 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)
  64.  
  65. '' Line and fill colors:
  66. Dim lines = New Color() {Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue}
  67. Dim line = 0
  68. Dim fills = New Color() {Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed}
  69. Dim fill = 0
  70.  
  71. '' The supported geometry types:
  72. Dim geoms As GeometryType() = [Enum].GetValues(GetType(GeometryType))
  73.  
  74. '' Add a paragraph and a run where the shapes will live:
  75. doc.Body.Paragraphs.Add("")
  76.  
  77. Dim shapes = New List(Of Shape)
  78. For Each g In geoms
  79. '' Line geometries do not support fills:
  80. If skipUnfillable AndAlso g.IsLineGeometry() Then
  81. Continue For
  82. End If
  83. If count = 0 Then
  84. Exit For
  85. End If
  86. count -= 1
  87.  
  88. Dim w = size.Width, h = size.Height
  89. Dim shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g)
  90. If Not g.IsLineGeometry() Then
  91. shape.Fill.Type = FillType.Solid
  92. If fill < fills.Length - 1 Then
  93. fill += 1
  94. Else
  95. fill = 0
  96. End If
  97. shape.Fill.SolidFill.RGB = fills(fill)
  98. End If
  99. shape.Line.Width = 3
  100. If line < lines.Length - 1 Then
  101. line += 1
  102. Else
  103. line = 0
  104. End If
  105. shape.Line.Fill.SolidFill.RGB = lines(line)
  106. If Not noNames AndAlso g.TextFrameSupported() Then
  107. shape.AddTextFrame(g.ToString())
  108. End If
  109. shape.AlternativeText = $"This is shape {g}"
  110. shape.Size.EffectExtent.AllEdges = 8
  111. shapes.Add(shape)
  112. Next
  113. Return shapes
  114. End Function
  115. End Class
  116.