Skip to content

Instantly share code, notes, and snippets.

@fileformat-words-gists
Last active March 3, 2024 10:36
Show Gist options
  • Save fileformat-words-gists/951eb4906ef57d8d6b93b1f009700a54 to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/951eb4906ef57d8d6b93b1f009700a54 to your computer and use it in GitHub Desktop.
C# Read 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);
System.Collections.Generic.List<FileFormat.Words.IElements.Paragraph>
paragraphs = body.Paragraphs;
foreach (FileFormat.Words.IElements.Paragraph paragraph in paragraphs)
{
System.Console.WriteLine($"Paragraph Text : {paragraph.Text}");
switch (paragraph.Indentation)
{
case var indent when indent.Left > 0:
System.Console.WriteLine($"Paragraph Left Indent: {indent.Left}");
break;
case var indent when indent.Right > 0:
System.Console.WriteLine($"Paragraph Right Indent: {indent.Right}");
break;
case var indent when indent.FirstLine > 0:
System.Console.WriteLine($"Paragraph Firstline Indent: {indent.FirstLine}");
break;
case var indent when indent.Hanging > 0:
System.Console.WriteLine($"Paragraph Hanging Indent: {indent.Hanging}");
break;
default:
System.Console.WriteLine("No Indentation.");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment