How to Set Word Built-in Style by Using C#/VB.NET

We often pay much attention on styles when writing papers in Word, such as title, heading, body settings and so on. Actually, Word provides users with various built-in styles. Users can use these styles directly or customize a new style according to their requirements. In this post, I will introduce a simple method to set Word style by using C#/VB.NET.

Style is used for paragraphs. Generally speaking, if one document has several parts, we may set a style for each part title, for example, heading 1 and set bodies as Normal style. Also, Word offers style for title and subtitle. In my example, I will show all the Word built-in Style in my document.

*The .NET Word Component, Spire.Doc for .NET is used in this method. If you want to use my code, please download it and add its dll file as reference in your project.

Detailed Step Shown as Following:

  1. Create a new Word document. Add section and add paragraph in this section.
  2. Add text in paragraph. The text is title to show what I will do next.
  3. Set font format for title, including font name, size, color and alignment.
  4. Set all the built-in styles. Because builtinStyle is enum type, so I use foreach sentence to show different styles in different paragraphs.
  5. Save and launch file.

Main Coding:

C#

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

namespace WordStyle
{
    class Style
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();

            //Add Section and Paragraph
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Add Builtin Style
            paragraph.AppendText(“Built-In Style”);

            //Font Format
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = “Title”;
            style.CharacterFormat.FontName = “Calibri”;
            style.CharacterFormat.FontSize = 18;
            style.CharacterFormat.TextColor = Color.DarkOrange;
            document.Styles.Add(style);
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
            paragraph.ApplyStyle(style.Name);

            //Style
            foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
            {
                paragraph = section.AddParagraph();

                //Append Text
                paragraph.AppendText(builtinStyle.ToString());

                //Apply Style
                paragraph.ApplyStyle(builtinStyle);
            }

            //Save and Launch
            document.SaveToFile(“Style.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Style.docx”);
        }
    }
}

VB.Net

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordStyle
    Friend Class Style
        Shared Sub Main(ByVal args() As String)
            ‘Create Document
            Dim document As New Document()
            ‘Add Section and Paragraph
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Add Builtin Style
            paragraph.AppendText(“Built-In Style”)

            ‘Font Format
            Dim style As New ParagraphStyle(document)
            style.Name = “Title”
            style.CharacterFormat.FontName = “Calibri”
            style.CharacterFormat.FontSize = 18
            style.CharacterFormat.TextColor = Color.DarkOrange
            document.Styles.Add(style)
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
            paragraph.ApplyStyle(style.Name)

            ‘Style
            For Each builtinStyle As BuiltinStyle In System.Enum.GetValues(GetType(BuiltinStyle))
                paragraph = section.AddParagraph()

                ‘Append Text
                paragraph.AppendText(builtinStyle.ToString())

                ‘Apply Style
                paragraph.ApplyStyle(builtinStyle)
            Next builtinStyle

            ‘Save and Launch
            document.SaveToFile(“Style.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Style.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

 

Download Spire.Doc for .NET Here

Advertisements

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