Set Word Font in C#, VB.NET

2011-01-13 08:01:53 Written by  Administrator
Rate this item
(0 votes)

Word Font setting allows users to change text font style and size in a document. With wonderful font settings, the document layout and appearance will be more appealed. What’s more, in order to make some special words, phrases or paragraphs more obvious, users can set different font styles or size. For example, the title is often set as different font style with bigger size from body.

Spire.Doc for .NET, a professional .NET Word component, enables users to set font in Word document. This guide will shows how to set by using C#, VB.NET via Spire.Doc for .NET and the following screenshot presents result after setting.

Word Text Font Style

  • Load the document and get the paragraph which you want to set font.
  • Then, declare a new paragraph style and set FontName and FontSize properties of this style.
  • Finally, apply the style for paragraph and save document. Code as following:
[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordImage
{
    class ImageinWord
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Humor Them.docx");

            //Get Paragraph
            Section s = document.Sections[0];
            Paragraph p = s.Paragraphs[1];

            //Set Font Style and Size
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "FontStyle";
            style.CharacterFormat.FontName = "Century Gothic";
            style.CharacterFormat.FontSize = 20;
            document.Styles.Add(style);
            p.ApplyStyle(style.Name);

            //Save and Launch
            document.SaveToFile("font.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("font.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordImage
    Friend Class ImageinWord
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Humor Them.docx")

            'Get Paragraph
            Dim s As Section = document.Sections(0)
            Dim p As Paragraph = s.Paragraphs(1)

            'Set Font Style and Size
            Dim style As New ParagraphStyle(document)
            style.Name = "FontStyle"
            style.CharacterFormat.FontName = "Century Gothic"
            style.CharacterFormat.FontSize = 20
            document.Styles.Add(style)
            p.ApplyStyle(style.Name)

            'Save and Launch
            document.SaveToFile("font.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("font.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, a professional Word component, can be used to generate, load, write, edit and save Word documents for .NET, Silverlight and WPF.

Additional Info

  • tutorial_title: Set Word Font
Last modified on Friday, 03 September 2021 03:59