ContentControlsHelpers.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.DrawingImports System.Collections.GenericImports System.LinqImports GrapeCity.Documents.Word
Public Class ContentControlsHelpers Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument() Dim p = doc.Body.AddParagraph("Content Control Examples", doc.Styles(BuiltInStyleId.Heading1)) p = doc.Body.AddParagraph( "Below are some examples of content controls. Open the document in MS Word to see the controls in action.", doc.Styles(BuiltInStyleId.Subtitle))
'' '' ContentControlType.DropdownList: '' doc.Body.AddParagraph("ContentControlType.DropdownList", doc.Styles(BuiltInStyleId.Heading2)) doc.Body.AddParagraph("Select a fruit from the dropdown list control: ", doc.Styles(BuiltInStyleId.Subtitle))
Dim dropDownListCtrl = p.AddContentControl(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"))
Dim dropDownPlaceholder = doc.GlossaryDocument.BuildingBlocks.Add("dropdownlist-placeholder", "General", type:=BuildingBlockType.ContentControlPlaceholder)
Dim pp = dropDownPlaceholder.Body.AddParagraph("Click to pick") pp.GetRange().Runs.First.Style = p.ParentBody.Document.Styles(BuiltInStyleId.Strong) pp.GetRange().Runs.First.Font.Color.RGB = Color.DarkSeaGreen
p.AddRun(" The green 'Click to pick' text on the left is a placeholder, replaced with the picked fruit when you've selected one.")
dropDownListCtrl.Color.RGB = Color.OrangeRed dropDownListCtrl.Font.Color.RGB = Color.DarkOrange dropDownListCtrl.ShowingPlaceholderText = True dropDownListCtrl.PlaceholderText = dropDownPlaceholder
'' '' ContentControlType.Text '' doc.Body.AddParagraph("ContentControlType.Text", doc.Styles(BuiltInStyleId.Heading2)) doc.Body.AddParagraph("ContentControlType.Text allows entering 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.", doc.Styles(BuiltInStyleId.Subtitle))
Dim textCtrl = doc.Body.AddContentControl(ContentControlType.Text, False) p = textCtrl.Content.AddParagraph( "This is the default content of the only run in the only paragraph in a Text content control.", doc.Styles(BuiltInStyleId.BlockText))
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 '' doc.Body.AddParagraph("ContentControlType.RichText", doc.Styles(BuiltInStyleId.Heading2)) doc.Body.AddParagraph("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " & "in a single RichText content control.", doc.Styles(BuiltInStyleId.Subtitle))
Dim richtext = doc.Body.AddContentControl(ContentControlType.RichText, False) Dim p1 = richtext.Content.AddParagraph("First paragraphs in the ") p1.AddRun("RichText ", doc.Styles(BuiltInStyleId.Strong)) p1.AddRun("content control.") Dim p2 = richtext.Content.AddParagraph("Second paragraphs in the ") p2.AddRun("RichText ", doc.Styles(BuiltInStyleId.Strong)) p2.AddRun("content control.")
p2.Style = doc.Styles(BuiltInStyleId.IntenseQuote)
'' '' ContentControlType.BuildingBlockGallery '' doc.Body.AddParagraph("ContentControlType.BuildingBlockGallery", doc.Styles(BuiltInStyleId.Heading2)) doc.Body.AddParagraph("ContentControlType.BuildingBlockGallery allows selecting items from " & "building block galleries. Here we allow to select an equation from the built-in Word equations gallery.", doc.Styles(BuiltInStyleId.Subtitle))
Dim galleryControl = doc.Body.AddContentControl(ContentControlType.BuildingBlockGallery) galleryControl.BuildingBlockCategory = "Built-In" galleryControl.BuildingBlockGallery = "Equations"
'' '' ContentControlType.Group '' doc.Body.AddParagraph("ContentControlType.Group", doc.Styles(BuiltInStyleId.Heading2)) p = doc.Body.AddParagraph("ContentControlType.Group allows createing groups with modifiable " & "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.", doc.Styles(BuiltInStyleId.Subtitle))
Dim groupControl = doc.Body.AddContentControl(ContentControlType.Group, False)
p = groupControl.Content.AddParagraph() Dim checkBox = p.AddContentControl(ContentControlType.CheckBox, True)
checkBox.Title = "Grouped checkbox" checkBox.Checked = True
p.AddRun(" Text to the right of the checkbox")
groupControl.Content.AddParagraph("Finally, we add a ContentControlType.Date control that shows the current date.")
Dim dt = groupControl.Content.AddContentControl(ContentControlType.Date, False) dt.DateFormat = "u" dt.Date = Util.TimeNow()
Return doc End FunctionEnd Class