C#/VB.NET – Get Paragraphs from Word by the Paragraph Style Name

The ability to retrieve paragraphs from a Word document is one of the essential qualities a Word library should have. Getting a paragraph by its style name can help developers accurately access a paragraph in a short time. This article shows you how to obtain paragraphs by their style names using Spire.Doc for .NET.

Spire.Doc offer the Paragraph.StyleName property to get the style name of a specific paragraph. The table below lists the style names supported by Spire.Doc as well as their corresponding names in MS Word. Some other style names in MS Word like “Emphasis”, “Subtitle Emphasis”, “Intense Emphasis”, “Strong”, “Subtitle Reference”, “Intense Reference”, and “Book Title” will be recognized as “Normal” by Spire.Doc.

Style Name in Spire.DocStyle Name in MS Word
TitleTitle
SubtitleSubtitle
Heading1Heading 1
Heading2Heading 2
Heading3Heading 3
NoSpacingNo Spacing
QuoteQuote
IntenseQuoteIntense Quote
ListParagraphList Paragraph
NormalNormal
Custom nameCustom name

Installing Spire.Doc

First of all, you need to download the latest version of Spire.Doc from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet.

Using the code

[C#]

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace GetParagraphs
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a sample Word document while initializing the Document object
            Document doc = new Document(@"C:\Users\Administrator\Desktop\Styles.docx");

            //Loop through the sections
            foreach (Section section in doc.Sections)
            {

                //Loop through the paragraphs
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    //Determine if the paragraph style name is 'Heading1'
                    if (paragraph.StyleName == "Heading1")
                    {
                        //Print out the paragraph content
                        Console.WriteLine("Heading1: " + paragraph.Text);
                    }

                    //Determine if the paragraph style name is 'Underline'
                    if (paragraph.StyleName == "Underline")
                    {
                        //Print out the paragraph content
                        Console.WriteLine("\n" + "Underline: " + paragraph.Text);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

[VB.NET]

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents
 
Namespace GetParagraphs
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Load a sample Word document while initializing the Document object
            Document doc  =  New Document("C:\Users\Administrator\Desktop\Styles.docx")
 
            'Loop through the sections
            Dim section As Section
            For Each section In doc.Sections
 
                'Loop through the paragraphs
                Dim paragraph As Paragraph
                For Each paragraph In section.Paragraphs
                    'Determine if the paragraph style name is 'Heading1'
                    If paragraph.StyleName = "Heading1" Then
                        'Print out the paragraph content
                        Console.WriteLine("Heading1: " + paragraph.Text)
                    End If
 
                    'Determine if the paragraph style name is 'Underline'
                    If paragraph.StyleName = "Underline" Then
                        'Print out the paragraph content
                        Console.WriteLine("\n" + "Underline: " + paragraph.Text)
                    End If
                Next
            Next
            Console.ReadKey()
        End Sub
    End Class
End Namespace

OUTPUT

Leave a comment