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.