Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not add bullets for word using OpenXml

My expected result is:

  • Hello
  • world!

but when i using below codes:

        MainDocumentPart mainDocumentPart =
          package.AddMainDocumentPart();

        DocumentFormat.OpenXml.Wordprocessing.Document elementW =
          new DocumentFormat.OpenXml.Wordprocessing.Document(
            new Body(
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                    new NumberingProperties(
                      new NumberingLevelReference() { Val = 0 },
                      new NumberingId() { Val = 1 })
                    ),
                new Run(
                  new RunProperties(),
                  new Text("Hello, ") { Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" } })),
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                new ParagraphProperties(
                  new NumberingProperties(
                    new NumberingLevelReference() { Val = 0 },
                    new NumberingId() { Val = 1 })),
                new Run(
                  new RunProperties(),
                  new Text("world!")
                  {
                      Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
                  })));

        elementW.Save(mainDocumentPart);

Result is:

  1. Hello
  2. world!

How can i get my expected result?

like image 923
OZnew Avatar asked Mar 27 '26 00:03

OZnew


1 Answers

I realize this is far too late but maybe it can help others with the same question. The marked answer (by amurra) doesn't actually achieve the desired result. It simply creates a document with the list as content, just more completely than you. What you have added to the main document part is fine.

In the XML format, list items are defined as paragraphs with an indentation level and a numbering ID. This ID references the numbering rules defined in the NumberingDefinitionsPart of the document.

In your case, because you've set the numbering ID to be 1, the following code would map that ID of 1 to reflect a bulleted list as desired. Note the NumberingFormat and LevelText objects inside the Level object. These are the key components for your formatting.

NumberingDefinitionsPart numberingPart =
    mainDocumentPart.AddNewPart<NumberingDefinitionsPart>("myCustomNumbering");

Numbering numElement = new Numbering(
    new AbstractNum(
        new Level(
            new NumberingFormat() { Val = NumberFormatValues.Bullet },
            new LevelText() { Val = "·" }
        ) { LevelIndex = 0 }
    ) { AbstractNumberId = 0 },
    new NumberingInstance(
        new AbstractNumId(){ Val = 0 }
    ){ NumberID = 1 }
);

numElement.Save(numberingPart);

For more information, check out the documentation for all the related classes on the Wordprocessing Namespace on MSDN, or the Working With Numbering markup article.

like image 163
Evan Steinkerchner Avatar answered Mar 30 '26 06:03

Evan Steinkerchner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!