private void DeleteSdtBlockAndKeepContent(MainDocumentPart mainDocumentPart, string sdtBlockTag)
        {
            List <SdtBlock> sdtList = mainDocumentPart.Document.Descendants <SdtBlock>().ToList();
            SdtBlock        sdtA    = null;

            foreach (SdtBlock sdt in sdtList)
            {
                if (sdt.SdtProperties.GetFirstChild <Tag>().Val.Value == sdtBlockTag)
                {
                    sdtA = sdt;
                    break;
                }
            }


            OpenXmlElement sdtc   = sdtA.GetFirstChild <SdtContentBlock>();
            OpenXmlElement parent = sdtA.Parent;

            OpenXmlElementList elements = sdtc.ChildElements;

            var mySdtc = new SdtContentBlock(sdtc.OuterXml);

            foreach (OpenXmlElement elem in elements)
            {
                string text = parent.FirstChild.InnerText;
                parent.Append((OpenXmlElement)elem.Clone());
            }

            sdtA.Remove();
        }
Beispiel #2
0
    public static Paragraph ReplaceToParagraph(this SdtBlock Block, string?Content = null)
    {
        var paragraph = Block.SdtContentBlock !.GetFirstChild <Paragraph>() !;

        paragraph.Remove();

        if (Content is not null)
        {
            paragraph.GetFirstChild <Run>() !.Text(Content);
        }

        Block.InsertAfterSelf(paragraph);
        Block.Remove();

        return(paragraph);
    }