ContentControls.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports GrapeCity.Documents.Word

'' This sample demonstrates how to add content controls to a document.
Public Class ContentControls
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()
        '' Heading:
        Dim p = doc.Body.Paragraphs.Add("Content Control Examples")
        p.Style = doc.Styles(BuiltInStyleId.Heading1)
        p = doc.Body.Paragraphs.Add(
                "Below are some examples of content controls. Open the document in MS Word to see the controls in action.")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        ''
        '' ContentControlType.DropdownList:
        ''
        p = doc.Body.Paragraphs.Add("ContentControlType.DropdownList")
        p.Style = doc.Styles(BuiltInStyleId.Heading2)
        p = doc.Body.Paragraphs.Add("Select a fruit from the dropdown list control: ")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        Dim dropDownListCtrl = p.GetRange().ContentControls.Add(ContentControlType.DropdownList, False)
        dropDownListCtrl.DropDownItems.Add(New DropDownItem("apple", "Apples"))
        dropDownListCtrl.DropDownItems.Add(New DropDownItem("orange", "Oranges"))
        dropDownListCtrl.DropDownItems.Add(New DropDownItem("banana", "Bananas"))
        dropDownListCtrl.DropDownItems.Add(New DropDownItem("pear", "Pears"))

        '' Add placeholder to the building blocks gallery:
        Dim dropDownPlaceholder = doc.GlossaryDocument.BuildingBlocks.Add(
            "dropdownlist-placeholder", "General", BuildingBlockGallery.Default,
            BuildingBlockInsertOptions.Content, BuildingBlockType.ContentControlPlaceholder)

        ''set placeholder text
        Dim pp = dropDownPlaceholder.Body.Paragraphs.Add("Click to pick")
        ''apply style to placeholder element
        pp.GetRange().Runs.First.Style = p.ParentBody.Document.Styles(BuiltInStyleId.Strong)
        ''...And change its color
        pp.GetRange().Runs.First.Font.Color.RGB = Color.DarkSeaGreen

        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.")

        '' Set control border color
        dropDownListCtrl.Color.RGB = Color.OrangeRed
        '' Set control text color:
        dropDownListCtrl.Font.Color.RGB = Color.DarkOrange

        '' Use building blocks placeholder on the DropDownList contentControl.           
        dropDownListCtrl.ShowingPlaceholderText = True
        dropDownListCtrl.PlaceholderText = dropDownPlaceholder

        ''
        ''  ContentControlType.Text
        ''
        p = doc.Body.Paragraphs.Add("ContentControlType.Text")
        p.Style = doc.Styles(BuiltInStyleId.Heading2)
        p = doc.Body.Paragraphs.Add("ContentControlType.Text allows to enter a single run of text in the control. " +
                "All text in a Text control has the same formatting. In this case we use 'Block Text' paragraph style.")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        Dim textCtrl = doc.Body.ContentControls.Add(ContentControlType.Text, False)
        p = textCtrl.Content.GetRange().Paragraphs.Add(
                "This is the default content of the only run in the only paragraph in a Text content control.",
                doc.Styles(BuiltInStyleId.BlockText))

        '' This code demonstrates that only one paragraph, And only one run in it,
        '' can exist in a Text control. Both statements under 'try' clauses will fail:
        Try
            textCtrl.Content.GetRange().Paragraphs.Add("Another paragraph (cannot be added).")
        Catch ex As InvalidContentControlSingleChildException
            Console.WriteLine("Cannot have more than one paragraph to a Text content control.")
        End Try
        Try
            p.GetRange().Runs.Add("Another run (cannot be added).")
        Catch ex As InvalidContentControlSingleChildException
            Console.WriteLine("Cannot have more than one run to a Text content control.")
        End Try

        ''
        '' ContentControlType.RichText
        ''
        p = doc.Body.Paragraphs.Add("ContentControlType.RichText")
        p.Style = doc.Styles(BuiltInStyleId.Heading2)
        p = doc.Body.Paragraphs.Add("ContentControlType.RichText allows to have multiple paragraphs, with multiple runs, " +
        "in a single RichText content control.")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        Dim richtext = doc.Body.ContentControls.Add(ContentControlType.RichText, False)
        Dim p1 = richtext.Content.GetRange().Paragraphs.Add("First paragraphs in the ")
        p1.GetRange().Runs.Add("RichText ", doc.Styles(BuiltInStyleId.Strong))
        p1.GetRange().Runs.Add("content control.")
        Dim p2 = richtext.Content.GetRange().Paragraphs.Add("Second paragraphs in the ")
        p2.GetRange().Runs.Add("RichText ", doc.Styles(BuiltInStyleId.Strong))
        p2.GetRange().Runs.Add("content control.")

        p2.Style = doc.Styles(BuiltInStyleId.IntenseQuote)

        ''
        '' ContentControlType.BuildingBlockGallery
        ''
        p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery")
        p.Style = doc.Styles(BuiltInStyleId.Heading2)
        p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery allows to select items from " +
        "building block galleries. Here we allow to select an equation from the built-in Word equations gallery.")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        '' We create a content control that allows to select equations from 
        '' the built-in Word equations gallery:
        Dim galleryControl = doc.Body.ContentControls.Add(ContentControlType.BuildingBlockGallery)
        galleryControl.BuildingBlockCategory = "Built-In"
        galleryControl.BuildingBlockGallery = "Equations"

        ''
        '' ContentControlType.Group
        ''
        p = doc.Body.Paragraphs.Add("ContentControlType.Group")
        p.Style = doc.Styles(BuiltInStyleId.Heading2)
        p = doc.Body.Paragraphs.Add("ContentControlType.Group allows to create groups with modifiable " +
        "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.")
        p.Style = doc.Styles(BuiltInStyleId.Subtitle)

        '' Group content control prevent some kinds of modifications of children controls.
        '' Add a group content control that will be used as parent container for
        '' child controls:
        Dim groupControl = doc.Body.ContentControls.Add(ContentControlType.Group, False)

        '' Note how to add ContentControls inside another ContentControl
        '' Do Not use groupControl.GetRange().ContentControls.Add(...),
        '' instead use groupControl.Content.GetRange().ContentControls.Add(...):
        p = groupControl.Content.GetRange().Paragraphs.Add()
        Dim checkBox = p.GetRange().ContentControls.Add(ContentControlType.CheckBox, True)

        checkBox.Title = "Grouped checkbox"
        checkBox.Checked = True

        p.GetRange().Runs.Add(" Text to the right of the checkbox")

        '' Date
        groupControl.Content.GetRange().Paragraphs.Add("Finally, we add a ContentControlType.Date control that shows the current date.")

        '' Add a Date control
        Dim dt = groupControl.Content.GetRange().ContentControls.Add(ContentControlType.Date, False)
        dt.DateFormat = "u"
        dt.Date = Util.TimeNow()

        '' In the Group contentControl, we have
        '' - a checkbox content control
        '' - a text to its left
        '' - a paragraph
        '' - a date content control
        '' Due to being inside Group content control, text cannot be changed unless
        '' you disable grouping (Word->Developer->Group And select Ungroup).

        '' Done
        Return doc
    End Function
End Class