首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在word中找到CheckBox内容控制并与Spire.Doc检查?

在word中找到CheckBox内容控制并与Spire.Doc检查?
EN

Stack Overflow用户
提问于 2022-04-30 18:23:23
回答 1查看 200关注 0票数 -1

我有一个word文件和内容控制复选框。如何使用sprire.doc查找这些复选框?

EN

回答 1

Stack Overflow用户

发布于 2022-05-05 03:04:35

下面的演示说明了如何查找复选框内容控件并使用Spire.Doc更新其复选状态。

代码语言:javascript
复制
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 { }
        }

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72071081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档