How to Manage Headings in Word Document with C#/VB.NET

In order to have a much clearer structure, we often separate an article into several parts and each part will have a title and all the contents are used to describe it. In Word document, the title for each part is called heading. Because of headings, the layout will be more wonderful and readers will learn what the author wants to present easily.

For setting headings, we just need to select title of one part in document and set its paragraph style as heading. Actually, a heading can include other headings, which can be taken as sub-levels of the main heading. For example, we can set heading 2 or heading 3 under heading 1. In this post, I want to share a method about how to manage Word Heading with C#, VB.NET.

I will create a blank Word document and add headings in it. Also, I will set a list format for them. In this example, I use a .NET Word component, Spire.Doc for .NET. So, I have added its dll file as reference in my project.

STEPS:

Step 1. Create Document

Create a new Word document and add section in it. Then, declare paragraph.

Step 2. Add Heading 1 and Heading 2

Add a paragraph and append text in it. Set the paragraph style as heading 1 and apply list number style for this paragraph. Then, add heading 2 as adding heading 1.

Step 3. Set Number Format for Headings

Declare list style 2 for heading 2. Use foreach sentence to get list level in list style and set number prefix for the levels. Then, add list style in document then apply for paragraph. Set list style 3 for heading 3 as setting for heading 2.

Step 4. Add Heading 3

Because I want to set several paragraphs as heading 3. So use a for loop to add paragraphs and set paragraph style as heading 3, apply list style 3 for paragraphs.

Step 5. Save and Launch

Save this document by using document.SaveToFile() method and the launch it for viewing.

C# Code

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace WordHeading

{

    class Heading

    {

        static void Main(string[] args)

        {

            //Create Document

            Document document = new Document();

            Section section = document.AddSection();

            Paragraph paragraph

                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

 

            //Heading 1

            paragraph = section.AddParagraph();

            paragraph.AppendText(BuiltinStyle.Heading1.ToString());

            paragraph.ApplyStyle(BuiltinStyle.Heading1);

            paragraph.ListFormat.ApplyNumberedStyle();

 

            //Heading 2

            paragraph = section.AddParagraph();

            paragraph.AppendText(BuiltinStyle.Heading2.ToString());

            paragraph.ApplyStyle(BuiltinStyle.Heading2);

 

            //Number Format(list) for Headings

            ListStyle listSty2 = new ListStyle(document, ListType.Numbered);

            foreach (ListLevel listLev in listSty2.Levels)

            {

                listLev.UsePrevLevelPattern = true;

                listLev.NumberPrefix = “1.”;

            }

            listSty2.Name = “MyStyle2”;

            document.ListStyles.Add(listSty2);

            paragraph.ListFormat.ApplyStyle(listSty2.Name);

         

            ListStyle listSty3 = new ListStyle(document, ListType.Numbered);

            foreach (ListLevel listLev in listSty3.Levels)

            {

                listLev.UsePrevLevelPattern = true;

                listLev.NumberPrefix = “1.1.”;

            }

            listSty3.Name = “MyStyle3”;

            document.ListStyles.Add(listSty3);

 

            //Heading 3

            for (int i = 0; i < 4; i++)

            {

                paragraph = section.AddParagraph();

 

                //Append Text

                paragraph.AppendText(“Heading3”);

 

                //Apply Style

                paragraph.ApplyStyle(BuiltinStyle.Heading3);

                paragraph.ListFormat.ApplyStyle(listSty3.Name);

            }

 

            //Save and Launch

            document.SaveToFile(“Word Headings.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“Word Headings.docx”);

        }

    }

}

 

VB Code

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace WordHeading

    Friend Class Heading

        Shared Sub Main(ByVal args() As String)

            ‘Create Document

            Dim document As New Document()

            Dim section As Section = document.AddSection()

            Dim paragraph As Paragraph = If(section.Paragraphs.Count > 0, section.Paragraphs(0), section.AddParagraph())

 

            ‘Heading 1

            paragraph = section.AddParagraph()

            paragraph.AppendText(BuiltinStyle.Heading1.ToString())

            paragraph.ApplyStyle(BuiltinStyle.Heading1)

            paragraph.ListFormat.ApplyNumberedStyle()

 

            ‘Heading 2

            paragraph = section.AddParagraph()

            paragraph.AppendText(BuiltinStyle.Heading2.ToString())

            paragraph.ApplyStyle(BuiltinStyle.Heading2)

 

            ‘Number Format(list) for Headings

            Dim listSty2 As New ListStyle(document, ListType.Numbered)

            For Each listLev As ListLevel In listSty2.Levels

                listLev.UsePrevLevelPattern = True

                listLev.NumberPrefix = “1.”

            Next listLev

            listSty2.Name = “MyStyle2”

            document.ListStyles.Add(listSty2)

            paragraph.ListFormat.ApplyStyle(listSty2.Name)

 

            Dim listSty3 As New ListStyle(document, ListType.Numbered)

            For Each listLev As ListLevel In listSty3.Levels

                listLev.UsePrevLevelPattern = True

                listLev.NumberPrefix = “1.1.”

            Next listLev

            listSty3.Name = “MyStyle3”

            document.ListStyles.Add(listSty3)

 

            ‘Heading 3

            For i As Integer = 0 To 3

                paragraph = section.AddParagraph()

 

                ‘Append Text

                paragraph.AppendText(“Heading3”)

 

                ‘Apply Style

                paragraph.ApplyStyle(BuiltinStyle.Heading3)

                paragraph.ListFormat.ApplyStyle(listSty3.Name)

            Next i

 

            ‘Save and Launch

            document.SaveToFile(“Word Headings.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“Word Headings.docx”)

        End Sub

    End Class

End Namespace

RESULT

DOWNLOAD (freely) Spire.Doc for .NET

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s