How to Work With Content Controls in Word Documents Using .NET

This technical tip explains how developers can work with content controls inside a MS Word documents using .NET. In Microsoft Word, you can create a form by starting with a template and adding content controls, including check boxes, text boxes, date pickers, and drop-down lists. In Aspose.Words, a Structured Document Tag or content control from any document loaded into Aspose.Words is imported as a StructuredDocumentTag node. Structured document tags (SDT or content control) allow to embed customer-defined semantics as well as its behavior and appearance into a document.
//your code here...//Code sample shows how to create content control of type rich text box. //[C# code sample] Document doc = new Document(); StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block); Paragraph para = new Paragraph(doc); Run run = new Run(doc); run.Text = "Hello World"; run.Font.Color = Color.Green; para.Runs.Add(run); sdtRichText.ChildNodes.Add(para); doc.FirstSection.Body.AppendChild(sdtRichText); doc.Save(MyDir + "Out.docx"); //[VB Code Sample] Dim doc As New Document() Dim sdtRichText As New StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block) Dim para As New Paragraph(doc) Dim run As New Run(doc) run.Text = "Hello World" run.Font.Color = Color.Green para.Runs.Add(run) sdtRichText.ChildNodes.Add(para) doc.FirstSection.Body.AppendChild(sdtRichText) doc.Save(MyDir & "Out.docx") //Code samples for how to create content control of type combo box. //[C# code sample] Document doc = new Document(); StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block); sdt.ListItems.Add(new SdtListItem("Choose an item", "-1")); sdt.ListItems.Add(new SdtListItem("Item 1", "1")); sdt.ListItems.Add(new SdtListItem("Item 2", "2")); doc.FirstSection.Body.AppendChild(sdt); doc.Save(MyDir + "Out.docx"); //[VB Code Sample] Dim doc As New Document() Dim sdt As New StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block) sdt.ListItems.Add(New SdtListItem("Choose an item", "-1")) sdt.ListItems.Add(New SdtListItem("Item 1", "1")) sdt.ListItems.Add(New SdtListItem("Item 2", "2")) doc.FirstSection.Body.AppendChild(sdt) doc.Save(MyDir & "Out.docx") //Code for how to modify content controls of type plain text box, drop down list and picture. //[C# code sample] Document doc = new Document(MyDir + "in.docx"); foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true)) { if (sdt.SdtType == SdtType.PlainText) { sdt.RemoveAllChildren(); Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph; Run run = new Run(doc, "new text goes here"); para.AppendChild(run); } else if (sdt.SdtType == SdtType.DropDownList) { SdtListItem secondItem = sdt.ListItems[2]; sdt.ListItems.SelectedValue = secondItem; } else if (sdt.SdtType == SdtType.Picture) { DrawingML dml = (DrawingML)sdt.GetChild(NodeType.DrawingML, 0, true); if (dml.HasImage) { dml.ImageData.SetImage(MyDir + "image.png"); } } } doc.Save(MyDir + "Out.docx"); //[VB Code Sample] Dim doc As New Document(MyDir & "in.docx") For Each sdt As StructuredDocumentTag In doc.GetChildNodes(NodeType.StructuredDocumentTag, True) If sdt.SdtType = SdtType.PlainText Then sdt.RemoveAllChildren() Dim para As Paragraph = TryCast(sdt.AppendChild(New Paragraph(doc)), Paragraph) Dim run As New Run(doc, "new text goes here") para.AppendChild(run) ElseIf sdt.SdtType = SdtType.DropDownList Then Dim secondItem As SdtListItem = sdt.ListItems(2) sdt.ListItems.SelectedValue = secondItem ElseIf sdt.SdtType = SdtType.Picture Then Dim dml As DrawingML = DirectCast(sdt.GetChild(NodeType.DrawingML, 0, True), DrawingML) If dml.HasImage Then dml.ImageData.SetImage(MyDir & "image.png") End If End If Next doc.Save(MyDir & "Out.docx")


Language: Java | User: Sheraz Khan | Created: Aug 21, 2014 | Tags: MS Word content controls Modify Content Controls in Word create Content Controls in Word Work With Content Controls in .NET use rich text box in word create combo box control C#