ContentControlsHelpers.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 how to add content controls to a document.
  15. // The code is the same as in the ContentControls example, but
  16. // uses the (new in v6.2) content creation helper methods,
  17. // which in most cases yield more robust and compact code.
  18. // Lines of code replaced by the new helper method calls are preserved
  19. // in the code of this example for reference, commented out with '//old:' comment.
  20. public class ContentControlsHelpers
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. var doc = new GcWordDocument();
  25. // Heading:
  26. //old: var p = doc.Body.Paragraphs.Add("Content Control Examples");
  27. //old: p.Style = doc.Styles[BuiltInStyleId.Heading1];
  28. var p = doc.Body.AddParagraph("Content Control Examples", doc.Styles[BuiltInStyleId.Heading1]);
  29. //old: p = doc.Body.Paragraphs.Add("Below are some examples of content controls. Open the document in MS Word to see the controls in action.");
  30. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  31. p = doc.Body.AddParagraph(
  32. "Below are some examples of content controls. Open the document in MS Word to see the controls in action.",
  33. doc.Styles[BuiltInStyleId.Subtitle]);
  34.  
  35. //
  36. // ContentControlType.DropdownList:
  37. //
  38. //old: p = doc.Body.Paragraphs.Add("ContentControlType.DropdownList");
  39. //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
  40. doc.Body.AddParagraph("ContentControlType.DropdownList", doc.Styles[BuiltInStyleId.Heading2]);
  41. //old: p = doc.Body.Paragraphs.Add("Select a fruit from the dropdown list control: ");
  42. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  43. doc.Body.AddParagraph("Select a fruit from the dropdown list control: ", doc.Styles[BuiltInStyleId.Subtitle]);
  44.  
  45. //old: var dropDownListCtrl = p.GetRange().ContentControls.Add(ContentControlType.DropdownList, false);
  46. var dropDownListCtrl = p.AddContentControl(ContentControlType.DropdownList, false);
  47. dropDownListCtrl.DropDownItems.Add(new DropDownItem("apple", "Apples"));
  48. dropDownListCtrl.DropDownItems.Add(new DropDownItem("orange", "Oranges"));
  49. dropDownListCtrl.DropDownItems.Add(new DropDownItem("banana", "Bananas"));
  50. dropDownListCtrl.DropDownItems.Add(new DropDownItem("pear", "Pears"));
  51.  
  52. // Add placeholder to the building blocks gallery:
  53. BuildingBlock dropDownPlaceholder = doc.GlossaryDocument.BuildingBlocks.Add("dropdownlist-placeholder", "General",
  54. type: BuildingBlockType.ContentControlPlaceholder);
  55.  
  56. //set placeholder text
  57. //old: Paragraph pp = dropDownPlaceholder.Body.Paragraphs.Add("Click to pick");
  58. var pp = dropDownPlaceholder.Body.AddParagraph("Click to pick");
  59. //apply style to placeholder element
  60. pp.GetRange().Runs.First.Style = p.ParentBody.Document.Styles[BuiltInStyleId.Strong];
  61. //...and change its color
  62. pp.GetRange().Runs.First.Font.Color.RGB = Color.DarkSeaGreen;
  63.  
  64. //old: p.GetRange().Runs.Add(" The green 'Click to pick' text on the left is a placeholder, replaced with the picked fruit when you've selected one.");
  65. p.AddRun(" The green 'Click to pick' text on the left is a placeholder, replaced with the picked fruit when you've selected one.");
  66.  
  67. // Set control border color:
  68. dropDownListCtrl.Color.RGB = Color.OrangeRed;
  69. // Set control text color:
  70. dropDownListCtrl.Font.Color.RGB = Color.DarkOrange;
  71.  
  72. // Use building blocks placeholder on the DropDownList contentControl.
  73. dropDownListCtrl.ShowingPlaceholderText = true;
  74. dropDownListCtrl.PlaceholderText = dropDownPlaceholder;
  75.  
  76. //
  77. // ContentControlType.Text
  78. //
  79. //old: p = doc.Body.Paragraphs.Add("ContentControlType.Text");
  80. //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
  81. doc.Body.AddParagraph("ContentControlType.Text", doc.Styles[BuiltInStyleId.Heading2]);
  82. //old: p = doc.Body.Paragraphs.Add("ContentControlType.Text allows entering a single run of text in the control. " +
  83. //old: "All text in a Text control has the same formatting. In this case we use 'Block Text' paragraph style.");
  84. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  85. doc.Body.AddParagraph("ContentControlType.Text allows entering a single run of text in the control. " +
  86. "All text in a Text control has the same formatting. In this case we use 'Block Text' paragraph style.",
  87. doc.Styles[BuiltInStyleId.Subtitle]);
  88.  
  89. //old: var textCtrl = doc.Body.ContentControls.Add(ContentControlType.Text, false);
  90. var textCtrl = doc.Body.AddContentControl(ContentControlType.Text, false);
  91. //old: p = textCtrl.Content.GetRange().Paragraphs.Add(
  92. //old: "This is the default content of the only run in the only paragraph in a Text content control.",
  93. //old: doc.Styles[BuiltInStyleId.BlockText]);
  94. p = textCtrl.Content.AddParagraph(
  95. "This is the default content of the only run in the only paragraph in a Text content control.",
  96. doc.Styles[BuiltInStyleId.BlockText]);
  97.  
  98. // This code demonstrates that only one paragraph, and only one run in it,
  99. // can exist in a Text control. Both statements under 'try' clauses will fail:
  100. try
  101. {
  102. textCtrl.Content.GetRange().Paragraphs.Add("Another paragraph (cannot be added).");
  103. }
  104. catch (InvalidContentControlSingleChildException)
  105. {
  106. Console.WriteLine("Cannot have more than one paragraph to a Text content control.");
  107. }
  108. try
  109. {
  110. p.GetRange().Runs.Add("Another run (cannot be added).");
  111. }
  112. catch (InvalidContentControlSingleChildException)
  113. {
  114. Console.WriteLine("Cannot have more than one run to a Text content control.");
  115. }
  116.  
  117. //
  118. // ContentControlType.RichText
  119. //
  120. //old: p = doc.Body.Paragraphs.Add("ContentControlType.RichText");
  121. //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
  122. doc.Body.AddParagraph("ContentControlType.RichText", doc.Styles[BuiltInStyleId.Heading2]);
  123. //old: p = doc.Body.Paragraphs.Add("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " +
  124. //old: "in a single RichText content control.");
  125. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  126. doc.Body.AddParagraph("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " +
  127. "in a single RichText content control.", doc.Styles[BuiltInStyleId.Subtitle]);
  128.  
  129. //old: var richtext = doc.Body.ContentControls.Add(ContentControlType.RichText, false);
  130. var richtext = doc.Body.AddContentControl(ContentControlType.RichText, false);
  131. //old: var p1 = richtext.Content.GetRange().Paragraphs.Add("First paragraphs in the ");
  132. var p1 = richtext.Content.AddParagraph("First paragraphs in the ");
  133. //old: p1.GetRange().Runs.Add("RichText ", doc.Styles[BuiltInStyleId.Strong]);
  134. p1.AddRun("RichText ", doc.Styles[BuiltInStyleId.Strong]);
  135. //old: p1.GetRange().Runs.Add("content control.");
  136. p1.AddRun("content control.");
  137. //old: var p2 = richtext.Content.GetRange().Paragraphs.Add("Second paragraphs in the ");
  138. var p2 = richtext.Content.AddParagraph("Second paragraphs in the ");
  139. //old: p2.GetRange().Runs.Add("RichText ", doc.Styles[BuiltInStyleId.Strong]);
  140. p2.AddRun("RichText ", doc.Styles[BuiltInStyleId.Strong]);
  141. //old: p2.GetRange().Runs.Add("content control.");
  142. p2.AddRun("content control.");
  143.  
  144. p2.Style = doc.Styles[BuiltInStyleId.IntenseQuote];
  145.  
  146. //
  147. // ContentControlType.BuildingBlockGallery
  148. //
  149. //old: p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery");
  150. //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
  151. doc.Body.AddParagraph("ContentControlType.BuildingBlockGallery", doc.Styles[BuiltInStyleId.Heading2]);
  152. //old: p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery allows selecting items from " +
  153. //old: "building block galleries. Here we allow to select an equation from the built-in Word equations gallery.");
  154. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  155. doc.Body.AddParagraph("ContentControlType.BuildingBlockGallery allows selecting items from " +
  156. "building block galleries. Here we allow to select an equation from the built-in Word equations gallery.",
  157. doc.Styles[BuiltInStyleId.Subtitle]);
  158.  
  159. // We create a content control that allows selecting equations from
  160. // the built-in Word equations gallery:
  161. //old: var galleryControl = doc.Body.ContentControls.Add(ContentControlType.BuildingBlockGallery);
  162. var galleryControl = doc.Body.AddContentControl(ContentControlType.BuildingBlockGallery);
  163. galleryControl.BuildingBlockCategory = "Built-In";
  164. galleryControl.BuildingBlockGallery = "Equations";
  165.  
  166. //
  167. // ContentControlType.Group
  168. //
  169. //old: p = doc.Body.Paragraphs.Add("ContentControlType.Group");
  170. //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
  171. doc.Body.AddParagraph("ContentControlType.Group", doc.Styles[BuiltInStyleId.Heading2]);
  172. //old: p = doc.Body.Paragraphs.Add("ContentControlType.Group allows createing groups with modifiable " +
  173. //old: "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.");
  174. //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
  175. p = doc.Body.AddParagraph("ContentControlType.Group allows createing groups with modifiable " +
  176. "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.",
  177. doc.Styles[BuiltInStyleId.Subtitle]);
  178.  
  179. // Group content control prevent some kinds of modifications of children controls.
  180. // Add a group content control that will be used as parent container for
  181. // child controls:
  182. //old: var groupControl = doc.Body.ContentControls.Add(ContentControlType.Group, false);
  183. var groupControl = doc.Body.AddContentControl(ContentControlType.Group, false);
  184.  
  185. // Note how to add ContentControls inside another ContentControl:
  186. // Do not use groupControl.GetRange().ContentControls.Add(...),
  187. // instead use groupControl.Content.GetRange().ContentControls.Add(...):
  188. //old: p = groupControl.Content.GetRange().Paragraphs.Add();
  189. p = groupControl.Content.AddParagraph();
  190. //old: var checkBox = p.GetRange().ContentControls.Add(ContentControlType.CheckBox, true);
  191. var checkBox = p.AddContentControl(ContentControlType.CheckBox, true);
  192.  
  193. checkBox.Title = "Grouped checkbox";
  194. checkBox.Checked = true;
  195.  
  196. //old: p.GetRange().Runs.Add(" Text to the right of the checkbox");
  197. p.AddRun(" Text to the right of the checkbox");
  198.  
  199. // Date:
  200. //old: groupControl.Content.GetRange().Paragraphs.Add("Finally, we add a ContentControlType.Date control that shows the current date.");
  201. groupControl.Content.AddParagraph("Finally, we add a ContentControlType.Date control that shows the current date.");
  202.  
  203. // Add a Date control
  204. //old: var date = groupControl.Content.GetRange().ContentControls.Add(ContentControlType.Date, false);
  205. var date = groupControl.Content.AddContentControl(ContentControlType.Date, false);
  206. date.DateFormat = "u";
  207. date.Date = Util.TimeNow();
  208.  
  209. // In the Group contentControl, we have:
  210. // - a checkbox content control
  211. // - a text to its left
  212. // - a paragraph
  213. // - a date content control
  214. // Due to being inside Group content control, text cannot be changed unless
  215. // you disable grouping (Word->Developer->Group and select Ungroup).
  216.  
  217. // Done:
  218. return doc;
  219. }
  220. }
  221. }
  222.