Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-words-gists/95efc98b9e1b579c3201309697c4ef97 to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/95efc98b9e1b579c3201309697c4ef97 to your computer and use it in GitHub Desktop.
C# Modify Word Paragraph Indent
// Prerequisite: Install <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
// Load the Word Document
var doc = new FileFormat.Words.Document($"filename.docx");
// Initialize the body with the document
var body = new FileFormat.Words.Body(doc);
foreach (FileFormat.Words.IElements.Paragraph paragraph in body.Paragraphs)
{
switch (paragraph.Indentation)
{
case var indent when indent.Left > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (left indent set to 0)", Italic = true });
paragraph.Indentation.Left = 0;
break;
case var indent when indent.Right > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (right indent set to 0)", Italic = true });
paragraph.Indentation.Right = 0;
break;
case var indent when indent.FirstLine > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (FirstLine indent set to 0)", Italic = true });
paragraph.Indentation.FirstLine = 0;
break;
case var indent when indent.Hanging > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (hanging indent set to 0)", Italic = true });
paragraph.Indentation.Hanging = 0;
break;
default:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (No indent and no change)", Italic = true });
break;
}
doc.Update(paragraph);
}
// Save the modified Word Document
doc.Save($"IndentModified.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment