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.Doc | Style Name in MS Word |
Title | Title |
Subtitle | Subtitle |
Heading1 | Heading 1 |
Heading2 | Heading 2 |
Heading3 | Heading 3 |
NoSpacing | No Spacing |
Quote | Quote |
IntenseQuote | Intense Quote |
ListParagraph | List Paragraph |
Normal | Normal |
Custom name | Custom 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