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

C# 正在寻找一种使用open xml将可折叠功能应用于word文档标题的方法,c#,.net,c#-4.0,ms-word,openxml,C#,.net,C# 4.0,Ms Word,Openxml,当我使用下面的代码以编程方式生成word文档时,我试图呈现标题部分 private static Paragraph BuildSubHeaderPart(string headingText) { Paragraph headerParagraph = new Paragraph(); var runProperty = new RunProperties(); runProperty.Append(new RunFonts(

当我使用下面的代码以编程方式生成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;
    }
将标题附加到正文中,如下所示

  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代码将
可折叠/扩展
功能应用于这个标题,如下图所示

有什么方法可以实现这个功能吗?我正在使用OpenXML和.NETCore来生成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);
段落标题Paragraph=新段落();
ParagraphProperties paragraphProperties1=新的ParagraphProperties();
OpenXmlUnknowneElement OpenXmlUnknowneElement1=OpenXmlUnknowneElement.CreateOpenXmlUnknowneElement(“”);
段落属性1.Append(OpenXMLUnknowneElement1);
var runProperty=新的RunProperties();
Append(新的runfont(){Ascii=“Nirmala UI”,HighAnsi=“Nirmala UI”}
,new FontSize{Val=new StringValue(“22”)}
,新加粗()
,新颜色(){Val=“55B6DA”});
Append(新文本(headingText){Space=SpaceProcessingModeValues.Default});
标题附件(第1段);
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);
ParagraphProperties paragraphProperties1=新的ParagraphProperties();
//水平仪使其可折叠
//您需要在{Val=1}中根据需要更改级别
//因此,要么需要将大纲级别的参数传递给
//方法并设置级别(或无级别)
//或者有第二种方法添加这些大纲级别的段落。
OutlineLevel OutlineLevel 1=新的OutlineLevel(){Val=1};
//使用w15:collapped元素使显示为
//打开文档时折叠。
//如果要在打开时展开,请不要添加
//这个元素
OpenXmlUnknowneElement OpenXmlUnknowneElement1=OpenXmlUnknowneElement.CreateOpenXmlUnknowneElement(“”);
段落属性1.附加(大纲级别1);
段落属性1.Append(OpenXMLUnknowneElement1);
headerParagraph.Append(段落属性);
如果您没有遇到Office Open XML生产力工具,您可能会发现它很有用:


[顺便说一句,我想你在这里描述的是标题。总而言之,标题是页面顶部的区域(底部有一个页脚)]

有谁能告诉我如何使用开放式xml向标题添加展开/折叠功能,或者如果这一切对输入没有意义,请告诉我,如何将此应用于现有标题。。你能给我指一下正确的方向吗。。我将我的代码放在
BuildSubHeaderPart
中,我想为正在使用此功能的标题添加可折叠功能。我更新了我的代码,但它不起作用,我无法看到标题附近的小三角形。。你能给我提个建议吗?我会留下我的第一个问题,然后想悬赏一下谢谢。。我也添加了更新后的代码,但仍然不起作用。非常感谢您更新这些代码。当我试图打开生成的文档时,我遇到了一个小问题,标题都折叠了,但没有展开。。我想在打开文档时展开所有标题。