C # read the Word content controls

In Word, by means of content control, document or template can be designed with specific functions. The following table introduces several common content control:

name

Brief Introduction

Drop-down list content control

Drop-down list contains a pre-defined list. And combo boxes difference is drop-down list does not allow users to edit entries.

Plain text content control

Plain text content control can only contain text, can not contain other items, such as tables, images, or other content controls.

Rich text content control

And plain text content of different controls, rich text content control can contain other items in addition to the text, such as tables, images, or other content controls.

Date Picker Content Control

Date Picker control contains the contents of a calendar control to help users enter the date.

Combo box content control

Combo box control contains a list can be edited directly. It combines the attributes of a text box and a drop-down list, the user can select the value or type value from the drop-down list box.

Picture content control

Picture content control to display the picture. The user can specify the picture when creating templates, you can also select the picture you want to insert by clicking the control.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This article describes the method to get the Word content controls already in the program by C #. Adding content controls in Word, please refer to this article .

Use tools: Spire.Doc for the .NET

dll file acquisition and import:

Method 1: through the official website download the dll file package. Once downloaded, extract the installation. After the installation, pay attention to add a reference Spire.Doc.dll vs assembly files in the program. As shown below:

Method 2: by Nuget download site.

 

Sample Code C # (for reference):

Test document as follows:

the using Spire.Doc;
 the using Spire.Doc.Documents;
 the using the System;
 the using the System.Collections.Generic;
 the using the System.Text; 

namespace GetSDT 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // load the contents of the control comprising documents 
            the document the document = new new the document (); 
            document.LoadFromFile ( " Test.docx " ); 

            // call StructureTags class to get a list of content controls 
            StructureTags structureTags =  getAllTags (the document);
            list<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

            //获取内容控件属性并输出到控制台
            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 (the Document the Document) 
        { 
            // traverse the document and get all the content controls 
            StructureTags structureTags = new new StructureTags ();
             foreach (Section Section in document.Sections) 
            { 
                foreach ( obj DocumentObject in section.Body.ChildObjects) 
                { 
                    IF (obj.DocumentObjectType == DocumentObjectType.Paragraph) 
                    { 
                        the 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 to read results:

 

 

(This article End)

 

Guess you like

Origin www.cnblogs.com/Yesi/p/11428831.html