Home > Code > Caching > How to Work With Content Controls in Word Documents in .NET Apps

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

by sher azam   on Aug 20, 2014   Category: Caching   |  Views: 2859    |  Points: 25   |  Gold 


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. StructuredDocumentTag can occur in a document in the following places:

• Block-level - Among paragraphs and tables, as a child of a Body, HeaderFooter, Comment, Footnote or a Shape node.
• Row-level - Among rows in a table, as a child of a Table node.
• Cell-level - Among cells in a table row, as a child of a Row node.
• Inline-level - Among inline content inside, as a child of a Paragraph.
• Nested inside another StructuredDocumentTag.

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

More about Aspose.Words for .NET

Aspose.Words is a word processing component that enables Java & .NET applications to read, write and modify Word documents without using Microsoft Word. Other useful features include document creation, content and formatting manipulation, mail merge abilities, reporting features, TOC updated/rebuilt, Embedded OOXML, Footnotes rendering and support of DOCX, DOC, WordprocessingML, HTML, XHTML, TXT and PDF formats (requires Aspose.Pdf). It supports both 32-bit and 64-bit operating systems. You can even use Aspose.Words to build applications with Mono.

More about Aspose.Words for .NET

- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.aspx

- Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts