Skip to content

Instantly share code, notes, and snippets.

@openize-words-gists
Last active May 22, 2024 12:42
Show Gist options
  • Save openize-words-gists/b2eebe46d7445258d2b9ba612d90b362 to your computer and use it in GitHub Desktop.
Save openize-words-gists/b2eebe46d7445258d2b9ba612d90b362 to your computer and use it in GitHub Desktop.
C# Read Word Document Paragraphs
// Prerequisite: Install <a href="https://www.nuget.org/packages/Openize.Words">Openize.Words</a>.
// Load the Word Document
var doc = new Openize.Words.Document($"WordParas.docx");
var body = new Openize.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment