Replacing Text of Content Controls in OpenXML

后端 未结 1 1686
臣服心动
臣服心动 2021-01-21 14:15

I have a word file that have multy Rich Text Content Control I want to change text of it. I Use this code .

 using (WordprocessingDocument theDoc =          


        
相关标签:
1条回答
  • 2021-01-21 14:46

    You only retrieve and update the first Text of your sdtContent. To replace all of it, the simples way is:

    • Delete all text
    • Add the new text

    Update your code with:

    if (alias != null)
    {
        // delete all paragraph of the sdt
        sdt.Descendants<Paragraph>().ToList().ForEach(p => p.Remove());
        // insert your new text, who is composed of:
        // - A Paragraph
        // - A Run
        // - A Text
        sdt.Append(new Paragraph(new Run(new Text("As a consultant his job is to design, develop and love poney."))));
    }
    

    edit: I forget to add the paragraph and the run

    You can see how is build a SdtContent here https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sdtcontentblock(v=office.14).aspx

    0 讨论(0)
提交回复
热议问题