7

So I have a document that I am editing through a WPF(C#) application. I have successfully edited Plain Text content controls, but now I am stuck checking/unchecking the Checkboxes within the form.

I successfully find the checkbox and set the value and save the document, but the checkboxes that are set to true are never checked within the word document when I open it.

Here is the code that I use to manipulate the checkboxes. NOTE: I access the checkboxes at the tag level, hence the field.parent.parent

private static void SetCheckBox(OpenXmlElement field, bool isChecked)
{
    var checkBox = field.Parent.Parent.Descendants<SdtContentCheckBox>().ToList();
    foreach (var check in checkBox)
    {
        if (isChecked)
        {
            check.Checked.Val = OnOffValues.True;
        }
        else
        {
            check.Checked.Val = OnOffValues.False;
        }
        MessageBox.Show(check.Checked.Val);
    }
}

When I display the values in the MessageBox they display 0/1 for true/false. So they are in fact being set.

Am I doing this properly?

2 Answers 2

9

So it seems that not only must the Checked value for a checkbox must be set, but so must the Text value be changed.

So my recent code has some alterations as well, but it changes bother aspects of the checkbox.

CODE:

private static void SetCheckBox(OpenXmlElement field, bool isChecked)
{
    if (isChecked)
    {
        field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = OnOffValues.True;
        field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = "☒";
    }
    else
    {
        field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = OnOffValues.False;
        field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = "☐";
    }
}

CONDENSED:

private static void SetCheckBox(OpenXmlElement field, bool isChecked)
{
    field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = isChecked ? OnOffValues.True : OnOffValues.False;
    field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = isChecked ? "☒" : "☐";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is this solving your problem ?
@MaximePorté yes it is
if this code solved the issue please mark it as the answer.
How on earth did you manage to get this working. Good stuff.
2

Another version of code to resolve the issue:

    private void ResetFile(string filePath)
    {
        using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
        {
            try
            {
                string uncheckValue = "☐";
                string checkValue = "☒";

                foreach (SdtContentCheckBox ctrl in doc.MainDocumentPart.Document.Body.Descendants<SdtContentCheckBox>())
                {
                    if (ctrl.Checked.Val == OnOffValues.One)
                    {
                        ctrl.Checked.Val = OnOffValues.Zero;
                        if (ctrl.Parent.Parent.Descendants<SdtContentRun>().ToList().Count > 0)
                        {
                            SdtContentRun text = (SdtContentRun)ctrl.Parent.Parent.Descendants<SdtContentRun>().ToList()[0];
                            text.InnerXml = text.InnerXml.Replace(checkValue, uncheckValue);
                        }
                    }
                }

                doc.MainDocumentPart.Document.Save();
            }
            catch { }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.