Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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();
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda