C#/VB.NET: Change the Font Style in a Word Document

Changing the font style in a Word document is an effective way to customize the appearance of your text. By applying different font styles, you can add visual interest and create a clear hierarchy within your content. This not only improves the overall quality of your document but also enhances the effectiveness of your communication. This article will demonstrate how to programmatically change the font style in a Word document using C# and VB.NET.

The following topics will be discussed:

  • Change the Font Style of a Specific Word in a Word Document
  • Change the Font Style of a Specific Paragraph in a Word Document
  • Change the Font Style of an Entire Word Document

Installation

To change the font style in a Word document, this article uses Free Spire.Doc for .NET. You can install Free Spire.Doc for .NET via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console, and then executing the following command:

PM> Install-Package FreeSpire.Doc

Alternatively, you can also download the DLL files of Free Spire.Doc for .NET from its official website, extract the package and then add the DLL files under the Bin folder to your project as references.

Change the Font Style of a Specific Word in a Word Document in C# and VB.NET

To change the font style of a specific word in a Word document, you need to search for the word in the document, then change the font style of all its occurrences.

The following steps explain how to change the font style of a specific word in a Word document:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile(string fileName) method.
  • Search for the word that you want to change the font color for using the Document.FindAllString(string matchString, bool caseSensitive, bool wholeWord) method.
  • Iterate through all the occurrences of the searched text.
  • Get the current occurrence as a single text range using the TextSelection.GetAsOneRange() method.
  • Change the font color of the text range using the TextRange.CharacterFormat.TextColor property.
  • Make the text of the text range bold using the TextRange.CharacterFormat.Bold property.
  • Save the result document using the Document.SaveToFile(string fileName, FileFormat fileFormat) method.

C#

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

namespace ChangeFontStyleForWord
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Search for the text that you want to change the font style for
            TextSelection[] text = document.FindAllString("Information", false, true);

            //Iterate through all the occurrences of the searched text
            foreach (TextSelection seletion in text)
            {
                //Get the current occurrence as a single text range
                TextRange range = seletion.GetAsOneRange();
                //Change the font color of the text range
                range.CharacterFormat.TextColor = Color.Red;
                //Make the text of the text range bold
                range.CharacterFormat.Bold = true;
            }

            //Save the result document
            document.SaveToFile("ChangeFontStyleForSpecificWord.docx", FileFormat.Docx2013);
            document.Dispose();
        }
    }
}

VB.NET

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

Namespace ChangeFontStyleForWord
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of the Document class
            Dim document As Document = New Document()
            'Load a Word document
            document.LoadFromFile("Sample.docx")

            'Search for the text that you want to change the font style for
            Dim text As TextSelection() = document.FindAllString("Information", False, True)

            'Iterate through all the occurrences of the searched text
            For Each seletion As TextSelection In text
                'Get the current occurrence as a single text range
                Dim range As TextRange = seletion.GetAsOneRange()
                'Change the font color of the text range
                range.CharacterFormat.TextColor = Color.Red
                'Make the text of the text range bold
                range.CharacterFormat.Bold = True
            Next

            'Save the result document
            document.SaveToFile("ChangeFontStyleForSpecificWord.docx", FileFormat.Docx2013)
            document.Dispose()
        End Sub
    End Class
End Namespace
Change the Font Style of Specific Word in Word Document using C# or VB.NET

Change the Font Style of a Specific Paragraph in a Word Document in C# and VB.NET

You can change the font style of a paragraph by creating a new paragraph style with a specific font style, then applying the new paragraph style to the paragraph you want to modify.

The following steps explain how to change the font style of a specific paragraph in a Word document:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile(string fileName) method.
  • Get a specific section of the document using the Document.Sections[int index] property.
  • Get a specific paragraph of the section using the Section.Paragraphs[int index] property.
  • Initialize an instance of the ParagraphStyle class to create a new paragraph style.
  • Set the name and font style for the paragraph style.
  • Add the paragraph style to the document using the Document.Styles.Add(IStyle style) method.
  • Apply the paragraph style to the specific paragraph using the Paragraph.ApplyStyle(string styleName) method.
  • Save the result document using the Document.SaveToFile(string fileName, FileFormat fileFormat) method.

C#

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

namespace ChangeFontStyleForParagraph
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Get the second paragraph
            Paragraph p1 = section.Paragraphs[1];

            //Create a new paragraph style with a specific font style
            ParagraphStyle paragraphStyle = new ParagraphStyle(document);
            paragraphStyle.Name = "ParaStyle";
            paragraphStyle.CharacterFormat.TextColor = Color.ForestGreen;
            paragraphStyle.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;

            //Add the paragraph style to the document
            document.Styles.Add(paragraphStyle);

            //Apply the paragraph style to the second paragraph
            p1.ApplyStyle(paragraphStyle.Name);

            //Save the result document
            document.SaveToFile("ChangeFontStyleForParagraph.docx", FileFormat.Docx2013);
            document.Dispose();
        }
    }
}

VB.NET

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

Namespace ChangeFontStyleForParagraph
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of the Document class
            Dim document As Document = New Document()
            'Load a Word document
            document.LoadFromFile("Sample.docx")

            'Get the first section
            Dim section As Section = document.Sections(0)

            'Get the second paragraph
            Dim p1 As Paragraph = section.Paragraphs(1)

            'Create a new paragraph style with a specific font style
            Dim paragraphStyle As ParagraphStyle = New ParagraphStyle(document)
            paragraphStyle.Name = "ParaStyle"
            paragraphStyle.CharacterFormat.TextColor = Color.ForestGreen
            paragraphStyle.CharacterFormat.UnderlineStyle = UnderlineStyle.[Single]

            'Add the paragraph style to the document
            document.Styles.Add(paragraphStyle)

            'Apply the paragraph style to the second paragraph
            p1.ApplyStyle(paragraphStyle.Name)

            'Save the result document
            document.SaveToFile("ChangeFontStyleForParagraph.docx", FileFormat.Docx2013)
            document.Dispose()
        End Sub
    End Class
End Namespace
Change the Font Style of Specific Paragraph in Word Document using C# or VB.NET

Change the Font Style of an Entire Word Document in C# and VB.NET

To change the font style of an entire Word document, you need to create a new paragraph style with a specific font style, then iterate through all the paragraphs in the document and apply the new paragraph style to them.

The following steps explain how to change the font style of an entire Word document:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile(string fileName) method.
  • Initialize an instance of the ParagraphStyle class to create a new paragraph style.
  • Set the name and font style for the paragraph style.
  • Add the paragraph style to the document using the Document.Styles.Add(IStyle style) method.
  • Iterate through all the sections in the document.
  • Iterate through all the paragraphs in each section.
  • Apply the paragraph style to each paragraph using the Paragraph.ApplyStyle(string styleName) method.
  • Save the result document using the Document.SaveToFile(string fileName, FileFormat fileFormat) method.

C#

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

namespace ChangeFontStyleForDocument
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Create a new paragraph style with specific font style
            ParagraphStyle paraStyle = new ParagraphStyle(document);
            paraStyle.Name = "ParaStyle";
            paraStyle.CharacterFormat.TextColor = Color.CornflowerBlue;
            paraStyle.CharacterFormat.Font = new Font("Arial", 12);
            paraStyle.CharacterFormat.Italic = true;

            //Add the paragraph style to the document
            document.Styles.Add(paraStyle);

            //Iterate through all the sections of the document
            foreach (Section section in document.Sections)
            {
                //Iterate through all the paragraphs of each section
                foreach (Paragraph para in section.Paragraphs)
                {
                    //Apply the paragraph style to each paragraph
                    para.ApplyStyle(paraStyle.Name);
                }
            }

            //Save the result document
            document.SaveToFile("ChangeFontStyleForEntireDocument.docx", FileFormat.Docx2013);
            document.Dispose();
        }
    }
}

VB.NET

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

Namespace ChangeFontStyleForDocument
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of the Document class
            Dim document As Document = New Document()
            'Load a Word document
            document.LoadFromFile("Sample.docx")

            'Create a new paragraph style with specific font style
            Dim paraStyle As ParagraphStyle = New ParagraphStyle(document)
            paraStyle.Name = "ParaStyle"
            paraStyle.CharacterFormat.TextColor = Color.CornflowerBlue
            paraStyle.CharacterFormat.Font = New Font("Arial", 12)
            paraStyle.CharacterFormat.Italic = True

            'Add the paragraph style to the document
            document.Styles.Add(paraStyle)

            'Iterate through all the sections of the document
            For Each section As Section In document.Sections
                'Iterate through all the paragraphs of each section
                For Each para As Paragraph In section.Paragraphs
                    'Apply the paragraph style to each paragraph
                    para.ApplyStyle(paraStyle.Name)
                Next
            Next

            'Save the result document
            document.SaveToFile("ChangeFontStyleForEntireDocument.docx", FileFormat.Docx2013)
            document.Dispose()
        End Sub
    End Class
End Namespace
Change the Font Style of Entire Word Document using C# or VB.NET

Leave a comment

Design a site like this with WordPress.com
Get started