Create a DOCX that shows shapes with all supported geometry types

DOCX PDF TIFF SVG JPG PNG C# VB
ShapeGeometryTypes.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 geometry types.
  15. public class ShapeGeometryTypes
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var doc = new GcWordDocument();
  20. var shapes = AddGeometryTypes(doc, new SizeF(100, 100));
  21. doc.Body.Paragraphs.Insert($"Shape geometry types ({shapes.Count})", doc.Styles[BuiltInStyleId.Title], InsertLocation.Start);
  22. return doc;
  23.  
  24. }
  25.  
  26. /// <summary>
  27. /// Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  28. /// The fill and line colors of the shapes are varied.
  29. /// </summary>
  30. /// <param name="doc">The target document.</param>
  31. /// <param name="size">The size of shapes to create.</param>
  32. /// <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  33. /// <param name="skipUnfillable">Add only shapes that support fills.</param>
  34. /// <param name="noNames">Do not add geometry names as shape text frames.</param>
  35. /// <returns>The list of shapes added to the document.</returns>
  36. private static List<Shape> AddGeometryTypes(GcWordDocument doc, SizeF size, int count = -1, bool skipUnfillable = false, bool noNames = false)
  37. {
  38. // Line and fill colors:
  39. Color[] lines = new Color[] { Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue, };
  40. int line = 0;
  41. Color[] fills = new Color[] { Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed, };
  42. int fill = 0;
  43.  
  44. // The supported geometry types:
  45. var geoms = Enum.GetValues(typeof(GeometryType));
  46.  
  47. // Add a paragraph and a run where the shapes will live:
  48. doc.Body.Paragraphs.Add("");
  49. Run run = doc.Body.Runs.Last;
  50.  
  51. var shapes = new List<Shape>();
  52. foreach (GeometryType g in geoms)
  53. {
  54. // Line geometries do not support fills:
  55. if (skipUnfillable && g.IsLineGeometry())
  56. continue;
  57.  
  58. if (count-- == 0)
  59. break;
  60.  
  61. float w = size.Width, h = size.Height;
  62. var shape = run.GetRange().Shapes.Add(w, h, g);
  63. if (!g.IsLineGeometry())
  64. {
  65. shape.Fill.Type = FillType.Solid;
  66. shape.Fill.SolidFill.RGB = fills[fill < fills.Length - 1 ? ++fill : (fill = 0)];
  67. }
  68. shape.Line.Width = 3;
  69. shape.Line.Fill.SolidFill.RGB = lines[line < lines.Length - 1 ? ++line : (line = 0)];
  70. if (!noNames && g.TextFrameSupported())
  71. shape.AddTextFrame(g.ToString());
  72. shape.AlternativeText = $"This is shape {g}";
  73. shape.Size.EffectExtent.AllEdges = 8;
  74. shapes.Add(shape);
  75. }
  76. return shapes;
  77. }
  78. }
  79. }
  80.