lundi 4 mai 2015

Unable to add check box in Run object of WordprocessingDocument

I have a document in which there is one Plain Text Box Content Control and a Checkbox control. I want to update the value of Text box and check box progrmmatically and generate new document.

My code is as per below:

static void Main(string[] args)
{
    string fileName = "C:\\xxx\\Template.docx";
    byte[] fileContent = File.ReadAllBytes(fileName);
    using (MemoryStream memStream = new MemoryStream())
    {
        memStream.Write(fileContent, 0, (int)fileContent.Length);
        WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStream, true);

        var sdtElements = wordDoc.MainDocumentPart.Document.Descendants<SdtElement>();
        //NewMethod(wordDoc);
        foreach (SdtElement sdtElement in sdtElements)
        {

            ContentControlType controlType = GetControlType(sdtElement);
            if (controlType == ContentControlType.PlainText)
            {
                string content = "RKS";
                Run nr = new Run();
                Text txt = new Text();
                txt.Text = content;
                nr.Append(txt);
                Lock lckContent = new Lock();
                if (true)
                {
                    lckContent.Val = LockingValues.SdtContentLocked;
                }
                else
                {
                    lckContent.Val = LockingValues.Unlocked;
                }

                if (sdtElement is SdtBlock)
                {
                    (((SdtBlock)sdtElement).SdtContentBlock.ElementAt(0)).RemoveAllChildren();
                    (((SdtBlock)sdtElement).SdtContentBlock.ElementAt(0)).AppendChild<Run>(nr);

                    ((SdtBlock)sdtElement).SdtProperties.Append(lckContent);
                }
                if (sdtElement is SdtCell)
                {
                    ((SdtCell)sdtElement).SdtContentCell.ElementAt(0).Descendants<Paragraph>().ElementAt(0).RemoveAllChildren();
                    ((SdtCell)sdtElement).SdtContentCell.ElementAt(0).Descendants<Paragraph>().ElementAt(0).AppendChild<Run>(nr);
                    ((SdtCell)sdtElement).SdtProperties.Append(lckContent);
                }
                if (sdtElement is SdtRun)
                {                            
                    ((SdtRun)sdtElement).SdtContentRun.ElementAt(0).RemoveAllChildren();
                    ((SdtRun)sdtElement).SdtContentRun.ElementAt(0).AppendChild<Run>(nr);
                    ((SdtRun)sdtElement).SdtProperties.Append(lckContent);
                }
            }
            else if (controlType == ContentControlType.CheckBox)
            {
                Run nr = new Run();                       
                //Text txt = new Text();
                //txt.Text = "checkbox";
                //nr.Append(txt);
                CheckBox checkbox = new CheckBox();                        
                nr.Append(checkbox);
                Lock lckContent = new Lock();
                if (true)
                {
                    lckContent.Val = LockingValues.SdtContentLocked;
                }
                else
                {
                    lckContent.Val = LockingValues.Unlocked;
                }

                if (sdtElement is SdtBlock)
                {
                    (((SdtBlock)sdtElement).SdtContentBlock.ElementAt(0)).RemoveAllChildren();
                    (((SdtBlock)sdtElement).SdtContentBlock.ElementAt(0)).AppendChild<Run>(nr);

                    ((SdtBlock)sdtElement).SdtProperties.Append(lckContent);
                }
                if (sdtElement is SdtCell)
                {
                    ((SdtCell)sdtElement).SdtContentCell.ElementAt(0).Descendants<Paragraph>().ElementAt(0).RemoveAllChildren();
                    ((SdtCell)sdtElement).SdtContentCell.ElementAt(0).Descendants<Paragraph>().ElementAt(0).AppendChild<Run>(nr);
                    ((SdtCell)sdtElement).SdtProperties.Append(lckContent);
                }
                if (sdtElement is SdtRun)
                {
                    ((SdtRun)sdtElement).SdtContentRun.ElementAt(0).RemoveAllChildren();
                    ((SdtRun)sdtElement).SdtContentRun.ElementAt(0).AppendChild<Run>(nr);
                    ((SdtRun)sdtElement).SdtProperties.Append(lckContent);
                }
            }
        }
        wordDoc.MainDocumentPart.Document.Save();
        File.WriteAllBytes("C:\\xxx\\Sample.docx", memStream.ToArray());
        wordDoc.Close();
    }
}

I am not able to place the check box using this code. What am I missing?




Aucun commentaire:

Enregistrer un commentaire