Table of Contents

Working With Word Paragraphs

In Openize.OpenXML-SDK for .NET, word paragraphs are handled by Paragraph class available under Openize.Words.IElements namespace. Paragraph class creates and/or loads word paragraphs via OpenXML SDK Paragraphs. At user end, using Paragraph class is super easy.

Creating a Normal Paragraph

Please follow below simple steps:

  • Initialize a new word document with the default template.
  • Initialize the body with the new document.
  • Create Normal Paragraph and include text runs with desired formatting.
  • Append paragraph to the body of the document.
  • Save the word document.

Code Example:

            // Initialize a new word document with the default template
            var doc = new Openize.Words.Document();
            // Initialize the body with the new document
            var body = new Openize.Words.Body(doc);
            // Create a Normal Paragraph
            var paragraph = new Openize.Words.IElements.Paragraph();
            // Define Run
            var run = new Openize.Words.IElements.Run
            {
                Text = $"Text in normal paragraph with default font and size but with bold " +
                           $"and underlined Gray Color ",
                Color = Openize.Words.IElements.Colors.Gray,
                Bold = true,
                Underline = true
            };
            // Add run to the paragraph
            paragraph.AddRun(run);
            // Append paragraph to the body of the word document
            body.AppendChild(paragraph);
            // Save the word document.
            doc.Save("normalPara.docx");

Creating a Heading Paragraph

Creating a heading paragraph requires to define the heading style. Users can use Heading1, Heading2,...Heading8 as defined in the default template. Style can be defined as property of the paragraph which uses "Normal" style by default.

Code Example:

            // Initialize a new word document with the default template
            var doc = new Openize.Words.Document();
            // Initialize the body with the new document
            var body = new Openize.Words.Body(doc);
            // Create an H1 heading Paragraphs
            var paragraphH1 = new Openize.Words.IElements.Paragraph
            {
                Style = "Heading1"
            };
            // Define Run
            var run = new Openize.Words.IElements.Run
            {
                Text = $"Paragraph with Heading1 Style"
            };
            // Add run to the H1 heading paragraph
            paragraphH1.AddRun(run);
            // Append paragraph to the body of the word document
            body.AppendChild(paragraphH1);
            // Save the word document.
            doc.Save("headingPara.docx");

Loading Word Paragraphs

Word paragraphs can be loaded via Paragraphs collection of Body of the word document. Below are the steps to follow:

  • Load the word document using Document class under Openize.Words namespace.
  • Initialize the Body with Document object.
  • Get the Paragraphs collection associated with the Body object.
  • Traverse through Paragraphs
  • Output each Paragraph and Run

Code Example

            // Load the Word Document
            var doc = new Openize.Words.Document($"normalPara.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;
            }

Modifying Word Paragraphs

Modifying word paragraphs require to load required paragraphs as demonstrated in the previous section. Modify paragraphs using Paragraph and Run classes as appropriate. Once targeted modifications are made, call Update method of Document object. Finally, once all modifications have been made, call Save method of Document object to save document to disk or stream.

Code Example

            // Load the Word Document
            var doc = new Openize.Words.Document($"normalPara.docx");
            var body = new Openize.Words.Body(doc);

            foreach (var paragraph in body.Paragraphs)
            {
                foreach (var run in paragraph.Runs)
                {
                    // Prepend 'Modified Heading :' for styled paragraphs
                    // and 'Modified Run :' for each run within normal paragraphs, preserving the existing format
                    run.Text = paragraph.Style.Contains("Heading") ? $"Modified Heading: {run.Text}" : $"Modified Run : {run.Text}";
                }
                // Update the paragraph in the document
                doc.Update(paragraph);
            }

            // Save the modified Word Document
            doc.Save($"ModifiedDoc.docx");
            System.Console.WriteLine($"Word Document normalPara.docx Modified and Saved As " +
                $"ModifiedDoc.docx.");