Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save openize-words-gists/5e903077f050411ab4ffc0848f838a34 to your computer and use it in GitHub Desktop.

Select an option

Save openize-words-gists/5e903077f050411ab4ffc0848f838a34 to your computer and use it in GitHub Desktop.
C# Create Multiple Multilevel List Paragraphs in Word Documents
// Prerequisite: Install <a href="https://www.nuget.org/packages/Openize.Words">Openize.Words</a>.
// The resulting docx document should be like this: https://i.imgur.com/6PKIb56.png
// Initialize docx document
var doc = new Openize.Words.Document();
System.Console.WriteLine("Word Document with default template initialized");
// Initialize document body
var body = new Openize.Words.Body(doc);
System.Console.WriteLine("Body of the Word Document initialized");
// Function to create a simple paragraph
void CreateParagraph(Openize.Words.Body body, string text)
{
var para = new Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.Words.IElements.Run { Text = text });
body.AppendChild(para);
}
// Function to create a list paragraph
void CreateListParagraph(Openize.Words.Body body, string text, int numberingId, string numberingType, int numberingLevel)
{
var para = new Openize.Words.IElements.Paragraph { Style = "ListParagraph" };
para.AddRun(new Openize.Words.IElements.Run { Text = text });
para.NumberingId = numberingId;
if (numberingType == "Number")
para.IsNumbered = true;
else if (numberingType == "Alphabet")
para.IsAlphabeticNumber = true;
else if (numberingType == "Roman")
para.IsRoman = true;
para.NumberingLevel = numberingLevel;
body.AppendChild(para);
}
// Create paragraphs
CreateParagraph(body, "This document is generated by Openize.Words.");
CreateParagraph(body, "Below is first multilevel list of paragraphs:");
CreateListParagraph(body, "First number at first level", 1, "Number", 1);
CreateListParagraph(body, "First alphabetic at second level", 1, "Alphabet", 2);
CreateListParagraph(body, "Second alphabetic at second level", 1, "Alphabet", 2);
CreateListParagraph(body, "First roman at third level", 1, "Roman", 3);
CreateListParagraph(body, "Second roman at third level", 1, "Roman", 3);
CreateListParagraph(body, "Second number at first level", 1, "Number", 1);
CreateParagraph(body, "The first multilevel list ends here...");
CreateParagraph(body, "Below is second multilevel list of paragraphs:");
CreateListParagraph(body, "First number at first level", 2, "Number", 1);
CreateListParagraph(body, "First roman at second level", 2, "Roman", 2);
CreateListParagraph(body, "Second roman at second level", 2, "Roman", 2);
CreateListParagraph(body, "First alphabet at third level", 2, "Alphabet", 3);
CreateListParagraph(body, "Second alphabet at third level", 2, "Alphabet", 3);
CreateListParagraph(body, "Second number at first level", 2, "Number", 1);
CreateParagraph(body, "The second multilevel list ends here...");
CreateParagraph(body, "The document ends here...");
// Save docx document to the disk
doc.Save($"MultilevelLists.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment