Created
May 16, 2024 17:45
-
-
Save fileformat-words-gists/28b1e2ba2d553059a2b85031208a2a2a to your computer and use it in GitHub Desktop.
C# Create Multiple Multilevel List Paragraphs in Word Documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prerequisite: Install <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>. | |
// The resulting docx document should be like this: https://i.imgur.com/6PKIb56.png | |
// Initialize docx document | |
FileFormat.Words.Document doc = new FileFormat.Words.Document(); | |
System.Console.WriteLine("Word Document with default template initialized"); | |
// Initialize document body | |
var body = new FileFormat.Words.Body(doc); | |
System.Console.WriteLine("Body of the Word Document initialized"); | |
// Function to create a simple paragraph | |
void CreateParagraph(FileFormat.Words.Body body, string text) | |
{ | |
var para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = text }); | |
body.AppendChild(para); | |
} | |
// Function to create a list paragraph | |
void CreateListParagraph(FileFormat.Words.Body body, string text, int numberingId, string numberingType, int numberingLevel) | |
{ | |
var para = new FileFormat.Words.IElements.Paragraph { Style = "ListParagraph" }; | |
para.AddRun(new FileFormat.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 FileFormat.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