簡體   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

我正在嘗試將富文本內容控件的內容從一個復制到另一個 Word 文檔。 每個富文本內容控件都包含一個文本塊和一些純文本內容控件。 下面的代碼似乎工作......

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

但是當我嘗試使用下面的代碼在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();

我找不到從sourceFile復制的富文本內容控件中的內容。 換句話說,我只想復制富文本內容控件的內容,而不復制控件本身。

更新:

為了簡化問題。 我有一個富文本內容控件,它可能有純文本和幾個純文本內容控件。 我只需要復制(僅)這個富文本內容控件中的內容,它將整個內容包裝到另一個 Word 文檔中。

與此同時,我自己設法解決了這個問題。 這是解決方案,因此它可能對其他人有用。

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