ShapeGeometryTypes.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 all available shape geometry types.
  13. public class ShapeGeometryTypes
  14. Function CreateDocx() As GcWordDocument
  15. Dim doc = New GcWordDocument()
  16. Dim shapes = AddGeometryTypes(doc, new SizeF(100, 100))
  17. doc.Body.Paragraphs.Insert($"Shape geometry types ({shapes.Count})", doc.Styles(BuiltInStyleId.Title), InsertLocation.Start)
  18. Return doc
  19. End Function
  20.  
  21. ''' <summary>
  22. ''' Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  23. ''' The fill and line colors of the shapes are varied.
  24. ''' </summary>
  25. ''' <param name="doc">The target document.</param>
  26. ''' <param name="size">The size of shapes to create.</param>
  27. ''' <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  28. ''' <param name="skipUnfillable">Add only shapes that support fills.</param>
  29. ''' <param name="noNames">Do not add geometry names as shape text frames.</param>
  30. ''' <returns>The list of shapes added to the document.</returns>
  31. 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)
  32.  
  33. '' Line and fill colors:
  34. Dim lines = New Color() {Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue}
  35. Dim line = 0
  36. Dim fills = New Color() {Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed}
  37. Dim fill = 0
  38.  
  39. '' The supported geometry types:
  40. Dim geoms As GeometryType() = [Enum].GetValues(GetType(GeometryType))
  41.  
  42. '' Add a paragraph and a run where the shapes will live:
  43. doc.Body.Paragraphs.Add("")
  44. Dim run = doc.Body.Runs.Last
  45.  
  46. Dim shapes = New List(Of Shape)
  47. For Each g In geoms
  48. '' Line geometries do not support fills:
  49. If skipUnfillable AndAlso g.IsLineGeometry() Then
  50. Continue For
  51. End If
  52. If count = 0 Then
  53. Exit For
  54. End If
  55. count -= 1
  56.  
  57. Dim w = size.Width, h = size.Height
  58. Dim shape = run.GetRange().Shapes.Add(w, h, g)
  59. If Not g.IsLineGeometry() Then
  60. shape.Fill.Type = FillType.Solid
  61. If fill < fills.Length - 1 Then
  62. fill += 1
  63. Else
  64. fill = 0
  65. End If
  66. shape.Fill.SolidFill.RGB = fills(fill)
  67. End If
  68. shape.Line.Width = 3
  69. If line < lines.Length - 1 Then
  70. line += 1
  71. Else
  72. line = 0
  73. End If
  74. shape.Line.Fill.SolidFill.RGB = lines(line)
  75. If Not noNames AndAlso g.TextFrameSupported() Then
  76. shape.AddTextFrame(g.ToString())
  77. End If
  78. shape.AlternativeText = $"This is shape {g}"
  79. shape.Size.EffectExtent.AllEdges = 8
  80. shapes.Add(shape)
  81. Next
  82. Return shapes
  83. End Function
  84. End Class
  85.