Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Apr 30, 2022 6:37 pm

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

Ginner123
 
Posts: 3
Joined: Sat Apr 30, 2022 6:36 pm

Mon May 02, 2022 3:27 am

Hello,

Thanks for your inquiry.
Please refer to the following code for test. If you have any other questions, please feel free to contact us.
Code: Select all
            //load file
            //Create a document
            Document document = new Document();

            //Load the document from disk.
            document.LoadFromFile(@"..\..\..\..\..\..\Data\CheckBoxContentControl.docx");

            //Call StructureTags
            StructureTags structureTags = GetAllTags(document);

            //Create list
            List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

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

                //Update the status
                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);
            }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 04, 2022 2:44 pm

1.png

I have a word file with 1 content control checkbox with tag "Check_1". How to find it and check it?

Ginner123
 
Posts: 3
Joined: Sat Apr 30, 2022 6:36 pm

Thu May 05, 2022 3:17 am

Hello,

Thanks for you reply.
I think I know what you need. Please test the code below.
Code: Select all
        public static void findCheckBox(Document document, bool value)
        {
            //Travel sections in document
            foreach (Section section in document.Sections)
            {
                //Travel childobjects in section
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                   
                    if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                        {
                            //Get StructureDocumentTagInline
                            if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                            {
                                StructureDocumentTagInline tagInline = pobj as StructureDocumentTagInline;
                                if (tagInline.SDTProperties.SDTType.ToString().Equals("CheckBox"))
                                {
                                    //change the status of checkbox
                                    SdtCheckBox sdtbox = tagInline.SDTProperties.ControlProperties as SdtCheckBox;
                                    sdtbox.Checked = value;
                                }
                            }
                        }
                    }
                    else if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                    {
                        StructureDocumentTag tag = obj as StructureDocumentTag;
                        if (tag.SDTProperties.SDTType.ToString().Equals("CheckBox"))
                        {
                            //change the status of checkbox
                            SdtCheckBox sdtbox = tag.SDTProperties.ControlProperties as SdtCheckBox;
                            sdtbox.Checked = value;
                        }
                    }

                }
            }
        }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Sat May 07, 2022 2:04 am

1111111111111111111111111111133333.png

Thanks for support.
But I still have 1 question that when running the code, instead of showing the tick box as installed, it shows the letter R. Is there any way to fix it?

Ginner123
 
Posts: 3
Joined: Sat Apr 30, 2022 6:36 pm

Sat May 07, 2022 7:29 am

Hello,

Thanks for your feedback.
As for your situation, please try to add "sdtbox.CheckedStateCharacterCode = 9745;" before "sdtbox.Checked", as shown in the following full testing code. If there is still any issue after trying, please share your testing word file with us. And then we will further investigate it. You can attach it here or send it to us via emai(support@e-iceblue.com).
Code: Select all
   public static void findCheckBox(Document document, bool value)
   {
       //Travel sections in document
       foreach (Section section in document.Sections)
       {
           //Travel childobjects in section
           foreach (DocumentObject obj in section.Body.ChildObjects)
           {

               if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
               {
                   foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                   {
                       //Get StructureDocumentTagInline
                       if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                       {
                           StructureDocumentTagInline tagInline = pobj as StructureDocumentTagInline;
                           if (tagInline.SDTProperties.SDTType.ToString().Equals("CheckBox"))
                           {
                               //change the status of checkbox
                               SdtCheckBox sdtbox = tagInline.SDTProperties.ControlProperties as SdtCheckBox;
                               sdtbox.CheckedStateCharacterCode = 9745;
                               sdtbox.Checked = value;
                           }
                       }
                   }
               }
               else if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
               {
                   StructureDocumentTag tag = obj as StructureDocumentTag;
                   if (tag.SDTProperties.SDTType.ToString().Equals("CheckBox"))
                   {
                       //change the status of checkbox
                       SdtCheckBox sdtbox = tag.SDTProperties.ControlProperties as SdtCheckBox;
                       sdtbox.CheckedStateCharacterCode = 9745;
                       sdtbox.Checked = value;
                   }
               }

           }
       }
   }

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc