Find CheckBox content control in word and check with Spire.Doc?

468 Views Asked by At

I have a word file with content control checkboxes. How to find those checkboxes with sprire.doc ???

1

There are 1 best solutions below

0
On

The demo below explains how to find check box content controls and update their check states with Spire.Doc.

using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;

namespace UpdateCheckBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("CheckBoxContentControl.docx");


            //Get the content controls in the document
            StructureTags structureTags = GetAllTags(document);
            List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

            //Loop through the controls
            for (int i = 0; i < tagInlines.Count; i++)
            {
                //Get the control type
                string type = tagInlines[i].SDTProperties.SDTType.ToString();

                //Update the check state of check box
                if (type == "CheckBox")
                {
                    SdtCheckBox scb = tagInlines[i].SDTProperties.ControlProperties as SdtCheckBox;
                    if (scb.Checked)
                    {
                        scb.Checked = false;
                    }
                    else
                    {
                        scb.Checked = true;
                    }
                }

            }
            //Save the document
            document.SaveToFile("Output.docx", FileFormat.Docx);

            //Open the document
            WordDocViewer("Output.docx"); 
        }

        static StructureTags GetAllTags(Document document)
        {
            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;
                }
            }
        }
        private void WordDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

    }
}