ShapeFillFormats.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 the various fill formats
  15. // that can be used with shapes.
  16. public class ShapeFillFormats
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. Color[] gradColors = new Color[] {
  21. Color.Red,
  22. Color.Orange,
  23. Color.Yellow,
  24. Color.Green
  25. };
  26.  
  27. var patts = Enum.GetValues(typeof(PatternFillType));
  28.  
  29. var doc = new GcWordDocument();
  30. var shapes = AddGeometryTypes(doc, new SizeF(100, 120), patts.Length + 12, true, true);
  31.  
  32. foreach (var s in shapes)
  33. {
  34. s.Size.Width.Value *= 2;
  35. s.Size.Height.Value *= 2;
  36. }
  37.  
  38. doc.Body.Paragraphs.Insert("Fill formats", doc.Styles[BuiltInStyleId.Title], InsertLocation.Start);
  39.  
  40. int sIdx = 0;
  41.  
  42. var shape = shapes[sIdx++];
  43. shape.Fill.Type = FillType.Gradient;
  44. shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomLeftToTopRight);
  45. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  46. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
  47. shape.AddTextFrame("Linear grad");
  48.  
  49. shape = shapes[sIdx++];
  50. shape.Fill.Type = FillType.Gradient;
  51. shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomRightToTopLeft);
  52. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  53. shape.Fill.GradientFill.Stops[1].Position = 30;
  54. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
  55. shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
  56. shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
  57. shape.AddTextFrame("Linear grad");
  58.  
  59. shape = shapes[sIdx++];
  60. shape.Fill.Type = FillType.Gradient;
  61. shape.Fill.GradientFill.Type = GradientType.Circle; // not really needed if using ApplyPathDirection() method
  62. shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.FromBottomRight);
  63. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  64. shape.Fill.GradientFill.Stops[1].Position = 30;
  65. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
  66. shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
  67. shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
  68. shape.AddTextFrame("Circle grad");
  69.  
  70. shape = shapes[sIdx++];
  71. shape.Fill.Type = FillType.Gradient;
  72. shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.Center);
  73. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  74. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
  75. shape.AddTextFrame("Circle grad");
  76.  
  77. shape = shapes[sIdx++];
  78. shape.Fill.Type = FillType.Gradient;
  79. shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.Center);
  80. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  81. shape.Fill.GradientFill.Stops[1].Position = 30;
  82. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
  83. shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
  84. shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
  85. shape.AddTextFrame("Rectangle grad");
  86.  
  87. shape = shapes[sIdx++];
  88. shape.Fill.Type = FillType.Gradient;
  89. shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.FromTopRight);
  90. shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
  91. shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
  92. shape.AddTextFrame("Rectangle grad");
  93.  
  94. // Image fills:
  95. shape = shapes[sIdx++];
  96. shape.Fill.Type = FillType.Image;
  97. var bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "butterfly.jpg"));
  98. shape.Fill.ImageFill.SetImage(bytes, "image/jpeg");
  99. shape = shapes[sIdx++];
  100. shape.Fill.Type = FillType.Image;
  101. shape.Fill.ImageFill.FillType = ImageFillType.Tile;
  102. //use tile image as flipped both vertical and horizontal
  103. shape.Fill.ImageFill.Tile.Flip = TileFlipMode.HorizontalAndVertical;
  104. //scale image down horizontally to 30% and vertically to 25%
  105. shape.Fill.ImageFill.Tile.HorizontalScale = 30;
  106. shape.Fill.ImageFill.Tile.VerticalScale = 25;
  107. bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "gcd-hex-logo-80x80.png"));
  108. shape.Fill.ImageFill.SetImage(bytes, "image/png");
  109.  
  110. var r = shape.GetRange().Runs.Insert("\n\nPattern fills:\n", doc.Styles[BuiltInStyleId.Strong], InsertLocation.After);
  111. r.Font.Size *= 3;
  112.  
  113. // Pattern fills:
  114. foreach (var p in patts)
  115. {
  116. shape = shapes[sIdx++];
  117. shape.Size.Width.Value /= 3;
  118. shape.Size.Height.Value /= 3;
  119. shape.Fill.Type = FillType.Pattern;
  120. //type represents how it will look
  121. shape.Fill.PatternFill.Type = (PatternFillType)p;
  122. shape.Fill.PatternFill.ForeColor.RGB = gradColors[0];
  123. shape.Fill.PatternFill.BackColor.RGB = gradColors[gradColors.Length - 1];
  124. }
  125.  
  126. if (sIdx < shapes.Count)
  127. shapes.Skip(sIdx).ToList().ForEach(s_ => s_.Delete());
  128.  
  129. return doc;
  130. }
  131.  
  132. /// <summary>
  133. /// Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
  134. /// The fill and line colors of the shapes are varied.
  135. /// </summary>
  136. /// <param name="doc">The target document.</param>
  137. /// <param name="size">The size of shapes to create.</param>
  138. /// <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
  139. /// <param name="skipUnfillable">Add only shapes that support fills.</param>
  140. /// <param name="noNames">Do not add geometry names as shape text frames.</param>
  141. /// <returns>The list of shapes added to the document.</returns>
  142. private static List<Shape> AddGeometryTypes(GcWordDocument doc, SizeF size, int count = -1, bool skipUnfillable = false, bool noNames = false)
  143. {
  144. // Line and fill colors:
  145. Color[] lines = new Color[] { Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue, };
  146. int line = 0;
  147. Color[] fills = new Color[] { Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed, };
  148. int fill = 0;
  149.  
  150. // The supported geometry types:
  151. var geoms = Enum.GetValues(typeof(GeometryType));
  152.  
  153. // Add a paragraph and a run where the shapes will live:
  154. doc.Body.Paragraphs.Add("");
  155.  
  156. var shapes = new List<Shape>();
  157. foreach (GeometryType g in geoms)
  158. {
  159. // Line geometries do not support fills:
  160. if (skipUnfillable && g.IsLineGeometry())
  161. continue;
  162.  
  163. if (count-- == 0)
  164. break;
  165.  
  166. float w = size.Width, h = size.Height;
  167. var shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g);
  168. if (!g.IsLineGeometry())
  169. {
  170. shape.Fill.Type = FillType.Solid;
  171. shape.Fill.SolidFill.RGB = fills[fill < fills.Length - 1 ? ++fill : (fill = 0)];
  172. }
  173. shape.Line.Width = 3;
  174. shape.Line.Fill.SolidFill.RGB = lines[line < lines.Length - 1 ? ++line : (line = 0)];
  175. if (!noNames && g.TextFrameSupported())
  176. shape.AddTextFrame(g.ToString());
  177. shape.AlternativeText = $"This is shape {g}";
  178. shape.Size.EffectExtent.AllEdges = 8;
  179. shapes.Add(shape);
  180. }
  181. return shapes;
  182. }
  183. }
  184. }
  185.