How to Set Word Text Font with C#/VB.NET

Word Font setting plays an important role in Word formatting, which includes font style, size, color and other settings.

Actually, sometimes, we can find that there are different font styles in a Word document. For example, the title font style is Times New Roman with the bigger size, while, body font style is Calibri with smaller size. Besides, people often set bright color for notes or key words in a document for telling readers that they are important.

In this post, I will introduce a simple method to set Word font by using C# and VB.NET.  

Note: I use the .NET Word component, Spire.Doc for .NET in this method, so I add its dll file as reference in my project.

The following steps show details.

  1. Create a Word document and add one section.
  2. Add paragraphs in section and append text in paragraphs. I add three paragraphs. The first shows text with font styles. The second paragraph is blank. The third paragraph shows styles which are used in paragraph one. But there is default style except color for third paragraph.
  3. Set styles for paragraphs, including font name, size, underline style and color. When setting color, I set different color for first paragraph and last paragraph.
  4. Save and launch file.

Main Coding:

C#

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

namespace WordFont
{
    class StylenColor
    {
        static void Main(string[] args)
        {
            //Create Word
            Document document = new Document();

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

            //Append Text
            String text
                = “This example shows how to set font style and color in Word document,”
                + ” including Font Name, Size, Underline Style and Color.”;
            TextRange txtRange = paragraph.AppendText(text);
            Paragraph paragrpah1 = section.AddParagraph();
            Paragraph paragraph2 = section.AddParagraph();
            String text1
                = “*Font Name: Centry Gothic; Size: 15; Underline Style: Dash; Color: Dark Blue”;
            TextRange txtRange1 = paragraph2.AppendText(text1);

            //Font name
            txtRange.CharacterFormat.FontName = “Century Gothic”;

            //Size
            txtRange.CharacterFormat.FontSize = 15;

            //Underline
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;

            //Color
            txtRange.CharacterFormat.TextColor = Color.DarkBlue;
            txtRange1.CharacterFormat.TextColor = Color.Orange;

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

VB.NET

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

Namespace WordFont
    Friend Class StylenColor
        Shared Sub Main(ByVal args() As String)
            ‘Create Word
            Dim document As New Document()

            ‘Add Section and Paragraph
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Append Text
            Dim text As String = “This example shows how to set font style and color in Word document,” & ” including Font Name, Size, Underline Style and Color.”
            Dim txtRange As TextRange = paragraph.AppendText(text)
            Dim paragrpah1 As Paragraph = section.AddParagraph()
            Dim paragraph2 As Paragraph = section.AddParagraph()
            Dim text1 As String = “*Font Name: Centry Gothic; Size: 15; Underline Style: Dash; Color: Dark Blue”
            Dim txtRange1 As TextRange = paragraph2.AppendText(text1)

            ‘Font name
            txtRange.CharacterFormat.FontName = “Century Gothic”

            ‘Size
            txtRange.CharacterFormat.FontSize = 15

            ‘Underline
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash

            ‘Color
            txtRange.CharacterFormat.TextColor = Color.DarkBlue
            txtRange1.CharacterFormat.TextColor = Color.Orange

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

Result Shown by Following:

Download Spire.Doc for .NET Here

Advertisements

2 thoughts on “How to Set Word Text Font with C#/VB.NET

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