Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-words-gists/87676cf05f3fe03a30a8a08087ce3faf to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/87676cf05f3fe03a30a8a08087ce3faf to your computer and use it in GitHub Desktop.
C# Create Word Paragraph Indent
// Prerequisite: Install <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
// Initialize a new word document with the default template
var doc = new FileFormat.Words.Document();
System.Console.WriteLine("Word Document with default template initialized");
// Initialize the body with the new document
var body = new FileFormat.Words.Body(doc);
System.Console.WriteLine("Body of the Word Document initialized");
// Add paragraph with left indentation
FileFormat.Words.IElements.Paragraph para1 = new FileFormat.Words.IElements.Paragraph();
para1.AddRun(new FileFormat.Words.IElements.Run
{
Text = "First paragraph with left indentation"
});
// Setting the Paragraph Indent 2 inches to left.
para1.Indentation.Left = 2;
// Add paragraph with right indentation
FileFormat.Words.IElements.Paragraph para2 = new FileFormat.Words.IElements.Paragraph();
para2.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Second paragraph with right indentation"
});
// Setting the Paragraph Indent 2 inches to right.
para2.Indentation.Right = 2;
// Add paragraph with firstline indentation
FileFormat.Words.IElements.Paragraph para3 = new FileFormat.Words.IElements.Paragraph();
para3.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Third paragraph with firstline indentation"
});
// Setting the Paragraph Indent 2 inches to firstline.
para3.Indentation.FirstLine = 2;
// Add paragraph with hanging indentation
FileFormat.Words.IElements.Paragraph para4 = new FileFormat.Words.IElements.Paragraph();
para4.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Fourth paragraph with hanging indentation"
});
// Setting the Paragraph Indent 2 inches to hanging.
para4.Indentation.Hanging = 2;
// Append paragraphs to the document body
body.AppendChild(para1);
body.AppendChild(para2);
body.AppendChild(para3);
body.AppendChild(para4);
// Save the newly created Word Document.
doc.Save($"WordDocWithParaIndent.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment