Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-words-gists/ea4a47288075c796ff5dd6bb97fccf1f to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/ea4a47288075c796ff5dd6bb97fccf1f to your computer and use it in GitHub Desktop.
C# Create Numbered Paragraphs in Word Document
// Prerequisite: Install <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
// 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");
// Initialize a paragraph
var para = new FileFormat.Words.IElements.Paragraph();
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "This document is generated by FileFormat.Words." });
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph();
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "Below are numbered paragraphs:" });
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph { Style = "ListParagraph" };
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "First numbered at first level" });
// Set numbering for the paragraph
para.NumberingId = 1;
para.IsNumbered = true;
para.NumberingLevel = 1;
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph { Style = "ListParagraph" };
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "First numbered at second level" });
// Set numbering for the paragraph
para.NumberingId = 1;
para.IsNumbered = true;
para.NumberingLevel = 2;
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph { Style = "ListParagraph" };
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "Second numbered at second level" });
// Set numbering for the paragraph
para.NumberingId = 1;
para.IsNumbered = true;
para.NumberingLevel = 2;
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph { Style = "ListParagraph" };
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "Second numbered at first level" });
// Set numbering for the paragraph
para.NumberingId = 1;
para.IsNumbered = true;
para.NumberingLevel = 1;
// Append paragraph to the document body
body.AppendChild(para);
// Reset paragraph
para = new FileFormat.Words.IElements.Paragraph();
// Add a run to paragraph
para.AddRun(new FileFormat.Words.IElements.Run
{ Text = "The document ends here..." });
// Append paragraph to the doucment body
body.AppendChild(para);
// Save docx document to the disk
doc.Save($"WordParagraphsNumbered.docx");
// The resulting docx document should be like this: https://imgur.com/vjTJxw5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment