In Word, with the help of content control, documents or templates with specific functions can be designed. Several commonly used content controls are briefly introduced in the following table:
Name |
brief introduction |
Dropdown List Content Control |
The drop-down list contains a predefined list. Unlike combo boxes, drop-down lists do not allow users to edit items. |
Plain text content control |
Plain text content controls can only contain text and cannot contain other items, such as tables, pictures or other content controls. |
Rich text content control |
Unlike plain text content controls, rich text content controls can contain items other than text, such as tables, pictures, or other content controls. |
Date picker content control |
The date picker content control contains a calendar control to help users enter dates. |
Combo Box Content Control |
The combo box control contains a list that can be edited directly. It combines the attributes of text box and drop-down list. Users can enter values in the box or select values from the drop-down list. |
Picture Content Control |
Picture content controls are used to display pictures. Users can specify pictures when making templates, or they can select pictures to insert by clicking this control. |
This article will introduce how to get the existing content control in Word through C # program. To add content controls to Word, please refer to This article.
Use tools: Spire. Doc for. NET
dll file acquisition and import:
Method 1: Through the official website Download dll File package. After downloading, decompress and install. After the installation is completed, notice to add a reference Spire.Doc.dll assembly file to the vs program. As follows:
Method 2: Through Nuget Website download.
C# sample code (for reference):
The test documents are as follows:
using Spire.Doc; using Spire.Doc.Documents; using System; using System.Collections.Generic; using System.Text; namespace GetSDT { class Program { static void Main(string[] args) { //Load documents with content controls Document document = new Document(); document.LoadFromFile("test.docx"); //call StructureTags Class Gets a List of Content Controls StructureTags structureTags = GetAllTags(document); List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines; //Get the content control properties and output them to the console for (int i = 0; i < tagInlines.Count; i++) { string alias = tagInlines[i].SDTProperties.Alias; string tag = tagInlines[i].SDTProperties.Tag; string value = tagInlines[i].SDTContent.Text; Console.WriteLine(alias); Console.WriteLine(tag); Console.WriteLine(value); Console.WriteLine("_____________________"); } Console.ReadLine(); } static StructureTags GetAllTags(Document document) { //Traverse the document and get all the content controls StructureTags structureTags = new StructureTags(); foreach (Section section in document.Sections) { foreach (DocumentObject obj in section.Body.ChildObjects) { if (obj.DocumentObjectType == DocumentObjectType.Paragraph) { foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects) { if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline) { structureTags.tagInlines.Add(pobj as StructureDocumentTagInline); } } } } } return structureTags; } public class StructureTags { List<StructureDocumentTagInline> m_tagInlines; public List<StructureDocumentTagInline> tagInlines { get { if (m_tagInlines == null) m_tagInlines = new List<StructureDocumentTagInline>(); return m_tagInlines; } set { m_tagInlines = value; } } } } }
Content control read effect:
(End of this article)