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