Search code examples
c#ms-wordopenxmlopenxml-sdk

How to copy content of Rich Text Content Control from Word document and remove the control itself using Open XML SDK


I'm trying to copy the content of Rich Text Content Control(s) from one to another Word document. Each of Rich Text Content Control(s) contains a text block and a few of Plain Text Content Controls. The code below seems to work...

using (WordprocessingDocument doc = WordprocessingDocument.Open(destinationFile, true))
{
    MainDocumentPart mainPart = doc.MainDocumentPart;
    Dictionary<string, SdtBlock> sdtBlocks = getContentControlsFromDocument(sourceFile);
    foreach (KeyValuePair<string, SdtBlock> sdtBlock in sdtBlocks)
    {
        SdtElement control = mainPart.Document.Body.Descendants<SdtElement>().Where(r =>
        {
            var tag = r.SdtProperties.GetFirstChild<Tag>();
            return tag != null && tag.Val == sdtBlock.Key.ToLower();
        }).FirstOrDefault();

        SdtContentBlock cloneSdtContentBlock = (SdtContentBlock)sdtBlock.Value.Descendants<SdtContentBlock>().FirstOrDefault().Clone();
        control.Parent.InsertAfter(cloneSdtContentBlock, control);
        control.Remove();
    }
    mainPart.Document.Save();
}

but when I try to find all the Content Controls within the destinationFile using the code below

string key = "tag_name";
List<SdtElement> controls = mainPart.Document.Body.Descendants<SdtElement>().Where(r =>
{
    var tag = r.SdtProperties.GetFirstChild<Tag>();
    return tag != null && tag.Val == key.ToLower();
}).ToList();

I can't find the ones that were/are within the Rich Text Content Control copied from the sourceFile. In other words, I'd like to copy only the content of the Rich Text Content Control without the control itself.

Update:

To simplify the question. I have a Rich Text Content Control which may have plain text and a couple of Plain Text Content Controls. All I need is copying (only) the content within this Rich Text Content Control which wrappes the whole thing to an other Word document.


Solution

  • In the meantime I managed to solve the problem myself. Here is the solution so it might be useful to someone else.

    using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\tmp\test-1.docx", true))
        {
            MainDocumentPart mainPart = doc.MainDocumentPart;
            foreach (var conditialTemplate in conditionalTemplates)
            {
                List<SdtElement> controls = mainPart.Document.Body.Descendants<SdtElement>().Where(r =>
                {
                    var tag = r.SdtProperties.GetFirstChild<Tag>();
                    return tag != null && tag.Val == conditialTemplate.ToLower();
                }).ToList();
        
                foreach (var control in controls)
                {
                    if (control != null)
                    {
                        SdtProperties props = control.Elements<SdtProperties>().FirstOrDefault();
                        Tag tag = props.Elements<Tag>().FirstOrDefault();
                        Console.WriteLine("Tag: " + tag.Val);
        
                        string theRightBlock = "A";
        
                        SdtBlock theRightSdtBlock = GetTheRightConditionalSdtBlock(theRightBlock, tag.Val);
                        if (theRightSdtBlock != null)
                        {    
                            OpenXmlElement parent = control.Parent;
                            SdtBlock clone = new SdtBlock();
                            clone = (SdtBlock)theRightSdtBlock.Clone();
                            var elements = clone.GetFirstChild<SdtContentBlock>().ChildElements.ToList();
                            elements.Reverse();
                            elements.ForEach(child =>
                            {
                                parent.InsertAfter(child.Clone() as OpenXmlElement, control);
                            });
                            control.Remove();
                        }
                    }
                }
            }
            mainPart.Document.Save();
        }