简体   繁体   English

如何从 Word 文档中复制富文本内容控件的内容并使用 Open XML 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.我正在尝试将富文本内容控件的内容从一个复制到另一个 Word 文档。 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但是当我尝试使用下面的代码在destinationFile中找到所有内容控件时

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 .我找不到从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.我只需要复制(仅)这个富文本内容控件中的内容,它将整个内容包装到另一个 Word 文档中。

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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