Create a DOCX that shows shapes with all supported themed shape styles

DOCX PDF TIFF SVG JPG PNG C# VB
ThemedShapeStyles.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample demonstrates the available predefined themed shape styles
  16. // that are supported by DsWord.
  17. // We first generate a number of different shapes with varying fill
  18. // and line colors, then duplicate that shape, and apply a themed style
  19. // to the copy.
  20. public class ThemedShapeStyles
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. var styles = typeof(ThemedShapeStyle).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
  25. .Where(p_ => p_.FieldType == typeof(ThemedShapeStyle));
  26. int stylesCount = styles.Count();
  27.  
  28. var doc = new GcWordDocument();
  29.  
  30. // We will apply each preset to 2 consecutive shapes:
  31. var shapes = AddGeometryTypes(doc, new SizeF(100, 100), stylesCount * 2, true, true);
  32.  
  33. doc.Body.Paragraphs.Insert($"Themed Shape Styles ({stylesCount})", doc.Styles[BuiltInStyleId.Title], InsertLocation.Start);
  34.  
  35. if (shapes.Count() > stylesCount * 2)
  36. shapes.Skip(stylesCount).ToList().ForEach(s_ => s_.Delete());
  37.  
  38. int styleIdx = 0;
  39. int flop = 0;
  40. foreach (var s in shapes)
  41. {
  42. var shape = s.GetRange().CopyTo(s.GetRange(), InsertLocation.After).ParentObject as Shape;
  43. var style = styles.ElementAt((flop++ % 2 == 0) ? styleIdx : styleIdx++);
  44. // Apply the themed style to the shape:
  45. shape.ApplyThemedStyle((ThemedShapeStyle)style.GetValue(null));
  46. // Insert the style's name in front of the styled shape:
  47. shape.GetRange().Runs.Insert($"{style.Name}:", InsertLocation.Before);
  48. shape.GetRange().Runs.Insert("\n", InsertLocation.After);
  49. }
  50.  
  51. return doc;
  52. }
  53.  
  54. /// <summary>
  55. /// Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  56. /// The fill and line colors of the shapes are varied.
  57. /// </summary>
  58. /// <param name="doc">The target document.</param>
  59. /// <param name="size">The size of shapes to create.</param>
  60. /// <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  61. /// <param name="skipUnfillable">Add only shapes that support fills.</param>
  62. /// <param name="noNames">Do not add geometry names as shape text frames.</param>
  63. /// <returns>The list of shapes added to the document.</returns>
  64. private static List<Shape> AddGeometryTypes(GcWordDocument doc, SizeF size, int count = -1, bool skipUnfillable = false, bool noNames = false)
  65. {
  66. // Line and fill colors:
  67. Color[] lines = new Color[] { Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue, };
  68. int line = 0;
  69. Color[] fills = new Color[] { Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed, };
  70. int fill = 0;
  71.  
  72. // The supported geometry types:
  73. var geoms = Enum.GetValues(typeof(GeometryType));
  74.  
  75. // Add a paragraph and a run where the shapes will live:
  76. doc.Body.Paragraphs.Add("");
  77.  
  78. var shapes = new List<Shape>();
  79. foreach (GeometryType g in geoms)
  80. {
  81. // Line geometries do not support fills:
  82. if (skipUnfillable && g.IsLineGeometry())
  83. continue;
  84.  
  85. if (count-- == 0)
  86. break;
  87.  
  88. float w = size.Width, h = size.Height;
  89. var shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g);
  90. if (!g.IsLineGeometry())
  91. {
  92. shape.Fill.Type = FillType.Solid;
  93. shape.Fill.SolidFill.RGB = fills[fill < fills.Length - 1 ? ++fill : (fill = 0)];
  94. }
  95. shape.Line.Width = 3;
  96. shape.Line.Fill.SolidFill.RGB = lines[line < lines.Length - 1 ? ++line : (line = 0)];
  97. if (!noNames && g.TextFrameSupported())
  98. shape.AddTextFrame(g.ToString());
  99. shape.AlternativeText = $"This is shape {g}";
  100. shape.Size.EffectExtent.AllEdges = 8;
  101. shapes.Add(shape);
  102. }
  103. return shapes;
  104. }
  105. }
  106. }
  107.