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