寻找一种使用 open xml 将可折叠功能应用于 word 文档中的标题的方法

looking for a way to apply collapsible feature to headings in word document using open xml

当我使用以下代码以编程方式生成 word 文档时,我正在尝试呈现标题部分

    private static Paragraph BuildSubHeaderPart(string headingText)
    {
        Paragraph headerParagraph = new Paragraph();

        var runProperty = new RunProperties();

        runProperty.Append(new RunFonts() { Ascii = "Nirmala UI", HighAnsi = "Nirmala UI" }
            , new FontSize { Val = new StringValue("22") }
            , new Bold()
            , new Color() { Val = "55B6DA" });
        runProperty.Append(new Text(headingText) { Space = SpaceProcessingModeValues.Default });
        headerParagraph.AppendChild(new Run()).Append(runProperty);

        return headerParagraph;
    }

将标题附加到 body,如下所示

  var mainDocumentPart = wordDoc.AddMainDocumentPart();
  Document doc = new Document();
  mainDocumentPart.Document = doc;
  doc.Body = new Body();

  Body body = wordDoc.MainDocumentPart.Document.Body;
  body.AppendChild(BuildSubHeaderPart("MECHANICAL SYSTEMS"));

这工作正常,现在我正在寻找使用 c# 代码将 collapsible/Expand 功能应用于此标题的方法,如下图

有什么方法可以实现这个功能,我正在使用 Open XML 和 .Net Core 来生成 word 文档。

任何人都可以就此提出任何想法,非常感谢。

更新代码:

  Paragraph headerParagraph = new Paragraph();
        ParagraphProperties paragraphProperties1 = new ParagraphProperties();
        OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<w15:collapsed xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" />");
        paragraphProperties1.Append(openXmlUnknownElement1);
        var runProperty = new RunProperties();
        runProperty.Append(new RunFonts() { Ascii = "Nirmala UI", HighAnsi = "Nirmala UI" }
            , new FontSize { Val = new StringValue("22") }
            , new Bold()
            , new Color() { Val = "55B6DA" });
        runProperty.Append(new Text(headingText) { Space = SpaceProcessingModeValues.Default });
        headerParagraph.Append(paragraphProperties1);
        headerParagraph.AppendChild(new Run()).Append(runProperty);

您应该可以对段落使用类似这样的东西,但是如果您想要折叠 样式 的所有段落,您将需要使用类似的代码来修改样式。

ParagraphProperties paragraphProperties1 = new ParagraphProperties();


// having the level makes it collapsible
// You'll need to change the level as appropriate in { Val = 1 }
// so either you'll need to pass an outline level parameter to your
// method and set the level (or no level) 
//or perhaps have a second method to add these outline level paras.
OutlineLevel outlineLevel1 = new OutlineLevel() { Val = 1 };


//Having the w15:collapsed element makes the show as
// collapsed when you open the document.
// if you want it expanded on open, do not add
// this element
OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<w15:collapsed xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" />");
paragraphProperties1.Append(outlineLevel1);
paragraphProperties1.Append(openXmlUnknownElement1);
headerParagraph.Append(paragraphProperties);

如果您还没有遇到过 Office Open XML 生产力工具,您可能会发现它很有用:https://www.microsoft.com/en-us/download/details.aspx?id=30425

[顺便说一句,我想你在这里描述的是一个标题。在 Word 中,Header 是页面顶部的区域(底部有 Footer)]