Glory Raj
Glory Raj

Reputation: 17701

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

I am trying to render the heading part when i am generating the word document programmatically using the below code

    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;
    }

Appending the heading to body like as below

  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"));

and this is working fine, Now i am looking way to apply collapsible/Expand feature to this heading using the c# code that will looks like as below image

enter image description here

Is there any way i can achieve this feature, I am using Open XML with .Net Core to generate the word document.

Could any one suggest any idea on this one, Many thanks in advance.

Updated Code :

  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);

Upvotes: 0

Views: 1235

Answers (1)

user12750785
user12750785

Reputation:

You should be able to use something like this for a Paragraph, but if you want all paragraphs of a Style to be collapsed you will need to use similar code to modify the Style.

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);

If you haven't encountered the Office Open XML Productivity Tool, you may find it useful: https://www.microsoft.com/en-us/download/details.aspx?id=30425

[BTW, I think what you are describing here is a Heading. In Word, a Header is the area at the top of a page (with a Footer at the bottom)]

Upvotes: 1

Related Questions