Create a DOCX that shows shapes with all supported geometry types

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