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