Last active
December 16, 2023 17:49
C# Read Word Document 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
/// <summary> | |
/// Loads a Word Document with structured content using | |
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>. | |
/// Traverses paragraphs and displays associated styles as defined by the Word document template. | |
/// Traverses through each run (text fragment) within each paragraph and displays fragment values. | |
/// </summary> | |
/// <param name="documentDirectory"> | |
/// The directory where the Word Document to load is present | |
/// (default is the root of your project). | |
/// </param> | |
/// <param name="filename"> | |
/// The name of the Word Document file to load (default is "WordParagraphs.docx" - please replace it | |
/// with your word document present at the directory defined by documentDirectory). | |
/// </param> | |
public void ReadWordParagraphs(string documentDirectory = docsDirectory, string filename = "WordParagraphs.docx") | |
{ | |
try | |
{ | |
// Load the Word Document | |
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}"); | |
var body = new FileFormat.Words.Body(doc); | |
var num = 0; | |
System.Console.WriteLine("Paragraphs Plain Text"); | |
// Traverse and display paragraphs with plain text | |
foreach (var paragraph in body.Paragraphs) | |
{ | |
num++; | |
System.Console.WriteLine($" Paragraph Number: {num}"); | |
System.Console.WriteLine($" Paragraph Style: {paragraph.Style}"); | |
System.Console.WriteLine($" Paragraph Text: {paragraph.Text}"); | |
} | |
num = 0; | |
var runnum = 0; | |
System.Console.WriteLine("Paragraphs with formatting"); | |
// Traverse and display paragraphs with formatting details | |
foreach (var paragraph in body.Paragraphs) | |
{ | |
num++; | |
System.Console.WriteLine($" Paragraph Number: {num}"); | |
System.Console.WriteLine($" Paragraph Style: {paragraph.Style}"); | |
// Traverse and display runs within each paragraph | |
foreach (var run in paragraph.Runs) | |
{ | |
runnum++; | |
System.Console.WriteLine($" Text fragment ({num} - {runnum}): {run.Text}"); | |
System.Console.WriteLine($" Font fragment ({num} - {runnum}): {run.FontFamily}"); | |
System.Console.WriteLine($" Color fragment ({num} - {runnum}): {run.Color}"); | |
System.Console.WriteLine($" Size fragment ({num} - {runnum}): {run.FontSize}"); | |
System.Console.WriteLine($" Bold fragment ({num} - {runnum}): {run.Bold}"); | |
System.Console.WriteLine($" Italic fragment ({num} - {runnum}): {run.Italic}"); | |
System.Console.WriteLine($" Underline fragment ({num} - {runnum}): {run.Underline}"); | |
} | |
runnum = 0; | |
} | |
} | |
catch (System.Exception ex) | |
{ | |
throw new FileFormat.Words.FileFormatException("An error occurred.", ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment