Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-words-gists/fbef77098e338fc29d2e5ba5108f0169 to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/fbef77098e338fc29d2e5ba5108f0169 to your computer and use it in GitHub Desktop.
C# Create Roman Alphabetic Paragraphs in Word Documents
// 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 roman and alphabetic 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 roman at first level" });
// Set roman numbering for the paragraph
para.NumberingId = 1;
para.IsRoman = 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 alphabetic at second level" });
// Set alphabetic numbering for the paragraph
para.NumberingId = 1;
para.IsAlphabeticNumber = 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 alphabetic at second level" });
// Set alphabetic numbering for the paragraph
para.NumberingId = 1;
para.IsAlphabeticNumber = 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 roman at first level" });
// Set roman and indentation for the paragraph
para.NumberingId = 1;
para.IsRoman = 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($"WordParagraphsRomanAlphabetic.docx");
// The resulting docx document should be like this: https://imgur.com/d7ywVPG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment