Last active
May 15, 2024 04:50
-
-
Save fileformat-words/00026d9017d0e49d3b3f31468698731b to your computer and use it in GitHub Desktop.
C# Create DOCX Numbered Paragraphs
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
// Create a document with numbered paragraphs using FileFormat.Words (https://www.nuget.org/packages/FileFormat.Words) | |
// The resulting docx document should be like this: https://imgur.com/vjTJxw5 | |
// Initialize docx document | |
FileFormat.Words.Document doc = new FileFormat.Words.Document(); | |
// Initialize document body | |
FileFormat.Words.Body body = new FileFormat.Words.Body(doc); | |
// Initialize a paragraph | |
FileFormat.Words.IElements.Paragraph 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.IsNumbered = true; | |
para.NumberingId = 1; | |
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.IsNumbered = true; | |
para.NumberingId = 1; | |
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.IsNumbered = true; | |
para.NumberingId = 1; | |
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.IsNumbered = true; | |
para.NumberingId = 1; | |
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("NumberedParas.docx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment