Replacing Text of Content Controls in OpenXML

≯℡__Kan透↙ 提交于 2019-12-02 02:44:33
Maxime Porté

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!