简体   繁体   中英

How to know if Word.docx Check Box (checkboxsymboltype in c# ) is checked or not?

I have a word document (document.docx) that have more than checkbox.

在此处输入图像描述

I want to know which one is check.

I can get all the checkboxes in the document by using

using DocumentFormat.OpenXml.Wordprocessing;

WordprocessingDocument documentFormA = WordprocessingDocument.Open(formALocation, true);

var checkBoxs = documentFormA.MainDocumentPart.Document.Body.Descendants<CheckBoxSymbolType>();

But this is the results that I get在此处输入图像描述

I can't differentiate which one is selected and which one is not at all.

I've created a blank document and inserted 2 checkboxes in it from the developer tab. I then checked the first box and closed the document.

2 个复选框的图像

I am able to extract the value of the checkboxes like this:

using (var wordDoc = WordprocessingDocument.Open(@"C:\test\checkbox.docx", true))
{
    MainDocumentPart mainPart = wordDoc.MainDocumentPart;
    var document = mainPart.Document;
    
    var checkboxes = document.Body.Descendants<SdtContentCheckBox>();
    foreach (var checkbox in checkboxes)
    {
        Console.WriteLine(checkbox.Checked.Val);
    }
}

Which will output:

1

0

Hope this helps.

Note that SdtContentCheckbox is in DocumentFormat.OpenXml.Office2010.Word and not DocumentFormat.OpenXml.Wordprocessing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM