New method get alias, tag and id of content controls in a Word document in C#

Content controls provide a way for you to design documents. When you add a content control to a document, the control is identified by a border, a title, and temporary text that can provide instructions to the user, and can prevent users from editing or deleting protected sections of a document.

Bind parts of a document or template to data. You can bind content controls to database fields, managed objects in the .NET Framework, XML elements that are stored in the document, and other data sources.

This article illustrates how to get all controls and their properties including alias, id and tag via Spire.Doc with new method and replace the Barcode with another image.

Refer this article to check old method: Get alias, tag and id of content controls in a Word document in C#

The following is test file new.docx.

New method get alias, tag and id of content controls in a Word document in C#

Here are the steps:

Step 1: Create a new Word document and load the test file.

Document doc = new Document(@"new.docx");

Step 2: Create a list StructureDocument to store tags. Here, each content control will be identified by tag.

public class StructureTags
        {
            List m_structureDocumnt;
            public List StructureDocument
            {
                get
                {
                    if (m_structureDocumnt == null)
                        m_structureDocumnt = new List();

                    return m_structureDocumnt;
                }
            }

        }

Step 3: Use foreach sentence to get all tags in the Word document.

foreach (Section section in doc.Sections)
            {
                foreach (Body body in section.ChildObjects)
                {
                    ModifyBody(body);
                }
            }

Step 4: Show the properties of all controls.

List tagInlines = structureTags.StructureDocument;
            Console.WriteLine("Part1");
            for (int i = 0; i < tagInlines.Count; i++)
            {
                string alias = tagInlines[i].SDTProperties.Alias;   // Can be null or empty
                decimal id = tagInlines[i].SDTProperties.Id;
                string tag = tagInlines[i].SDTProperties.Tag;
                string STDType = tagInlines[i].SDTProperties.SDTType.ToString();

                Console.WriteLine("{0,20},{1,15},{2, 10} - {3}", alias, id, STDType, tag);
                Console.ReadKey();
             }

Step 5: Replace image inside of Picture Content Control.

doc.SaveToFile("replace1.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("replace1.docx");

Result Screenshot:

New method get alias, tag and id of content controls in a Word document in C#

New method get alias, tag and id of content controls in a Word document in C#

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace GetAlias
{

    class Program
    {
     
           static StructureTags structureTags = new StructureTags();
        static void Main(string[] args)
        {
            Document doc = new Document(@"new.docx");

            foreach (Section section in doc.Sections)
            {
                foreach (Body body in section.ChildObjects)
                {
                    ModifyBody(body);
                }
            }

            List tagInlines = structureTags.StructureDocument;
            Console.WriteLine("Part1");
            for (int i = 0; i < tagInlines.Count; i++)
            {
                string alias = tagInlines[i].SDTProperties.Alias;
                decimal id = tagInlines[i].SDTProperties.Id;
                string tag = tagInlines[i].SDTProperties.Tag;
                string STDType = tagInlines[i].SDTProperties.SDTType.ToString();

                Console.WriteLine("{0,20},{1,15},{2, 10} - {3}", alias, id, STDType, tag);
                Console.ReadKey();

                if (tagInlines[i].SDTProperties.SDTType == SdtType.Picture)
                {
                    DocPicture picture = tagInlines[i].ChildObjects.FirstItem as DocPicture;
                    if (picture == null)
                    {
                        picture = new DocPicture(doc);
                        picture.LoadImage(Image.FromFile(@"cat.jpg"));
                        tagInlines[i].ChildObjects.Add(picture);
                    }
                    else
                    {
                        picture.LoadImage(Image.FromFile(@"cat.jpg"));
                    }
                }
            }

            doc.SaveToFile("replace1.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("replace1.docx");
        }

        static void ModifyBody(Body body)
        {
            if (body == null)
                return;

            foreach (DocumentObject docObj in body.ChildObjects)
            {
                if (docObj is StructureDocumentTag)
                {
                    structureTags.StructureDocument.Add(docObj as StructureDocumentTag);

                }
                else if (docObj is Table)
                {
                    ModifyTable(docObj as Table);
                }
                else if (docObj is Paragraph)
                {
                    ModifyParagraph(docObj as Paragraph);
                }
            }
        }

        static void ModifyTable(Table table)
        {
            if (table == null)
                return;

            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    if (cell is StructureDocumentTagCell)
                    {
                        structureTags.StructureDocument.Add(cell as StructureDocumentTagCell);
                    }
                    else
                    {
                        ModifyBody(cell);
                    }
                }
            }
        }

        static void ModifyParagraph(Paragraph para)
        {
            if (para == null)
                return;

            foreach (DocumentObject docObj in para.ChildObjects)
            {
                if (docObj is StructureDocumentTagInline)
                {

                    structureTags.StructureDocument.Add(docObj as StructureDocumentTagInline);
                }
            }
        }

        public class StructureTags
        {
            List m_structureDocumnt;
            public List StructureDocument
            {
                get
                {
                    if (m_structureDocumnt == null)
                        m_structureDocumnt = new List();

                    return m_structureDocumnt;
                }
            }
        }

        }
    }