C#/VB.NET: How to Apply Formatting to Characters in Word

 Character format settings are used to control how characters are displayed and printed on the screen. The character formats in Word documents include: font, font size, font color, highlight color, border, underline, italic, shaded word, emphasis, etc. Readers can be drawn to important points of content by well-formatted text. Applying character formatting can make simple documents more attractive than plain text alone. This article will introduce you an efficient and convenient method to apply formatting to characters in Word programmatically. For your reference, we have attached the C#/VB.NET code.

Programming Environment

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Apply Formatting to Characters in Word

The steps to format characters in Word are as follows:

  • Create a Document object.
  • Add a section to the document using Document.AddSection() method.
  • Add a paragraph to the section using Section.AddParagraph() method.
  • Append text to the paragraph using Paragraph.AppendText() method and return a TextRange object.
  • Apply formatting such as font name, font size, border and highlight color to the characters within the text range through TextRange.CharacterFormat property.
  • Save the document to a Word file using Document.SaveToFile() method.

Full Code

C#

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

using System.Drawing;

namespace ApplyFormattingToCharacters

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a Document object

            Document document = new Document();

            //Add a section

            Section sec = document.AddSection();

            //Add a paragraph

            Paragraph paragraph = sec.AddParagraph();

            paragraph.AppendText("Here is a paragraph with various character styles. This is ");

            //Append text to the paragraph and return a TextRange object

            TextRange tr = paragraph.AppendText("text with strikeout");

            //Set the character format to strikeout via TextRange object

            tr.CharacterFormat.IsStrikeout = true;

            //Apply shadow effect to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text with shadow");

            tr.CharacterFormat.IsShadow = true;

            //Set font size

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text in a large font size");

            tr.CharacterFormat.FontSize = 20;

            //Set font name

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text in the font of Arial Black");

            tr.CharacterFormat.FontName = "Arial Black";

            //Set font color

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text in red");

            tr.CharacterFormat.TextColor = Color.Red;

            //Apply bold & italic to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text in bold & italic");

            tr.CharacterFormat.Bold = true;

            tr.CharacterFormat.Italic = true;

            //Apply underline to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("underlined text");

            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;

            //Apply background color to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text with highlight color");

            tr.CharacterFormat.HighlightColor = Color.Green;

            //Apply border to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text with border");

            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Single;

            tr.CharacterFormat.Border.Color = Color.Black;

            //Apply emphasis mark to text

            paragraph.AppendText(". This is ");

            tr = paragraph.AppendText("text with emphasis mark");

            tr.CharacterFormat.EmphasisMark = Emphasis.DotBelow;

            //Apply superscript to text

            paragraph.AppendText(". This is a math formula: a");

            tr = paragraph.AppendText("2");

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;

            paragraph.AppendText(" + b");

            tr = paragraph.AppendText("2");

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;

            paragraph.AppendText(" = c");

            tr = paragraph.AppendText("2");

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;

            paragraph.AppendText(".");

            //Save to file

            document.SaveToFile("SetCharacterFormat.docx", FileFormat.Docx);

        }

    }

}

VB.NET

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

Imports System.Drawing

Namespace ApplyFormattingToCharacters

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Create a Document object

            Dim document As Document = New Document()

            'Add a section

            Dim sec As Section = document.AddSection()

            'Add a paragraph

            Dim paragraph As Paragraph = sec.AddParagraph()

            paragraph.AppendText("Here is a paragraph with various character styles. This is ")

            'Append text to the paragraph and return a TextRange object

            Dim tr As TextRange = paragraph.AppendText("text with strikeout")

            'Set the character format to strikeout via TextRange object

            tr.CharacterFormat.IsStrikeout = True

            'Apply shadow effect to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text with shadow")

            tr.CharacterFormat.IsShadow = True

            'Set font size

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text in a large font size")

            tr.CharacterFormat.FontSize = 20

            'Set font name

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text in the font of Arial Black")

            tr.CharacterFormat.FontName = "Arial Black"

            'Set font color

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text in red")

            tr.CharacterFormat.TextColor = Color.Red

            'Apply bold & italic to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text in bold & italic")

            tr.CharacterFormat.Bold = True

            tr.CharacterFormat.Italic = True

            'Apply underline to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("underlined text")

            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.[Single]

           'Apply background color to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text with highlight color")

            tr.CharacterFormat.HighlightColor = Color.Green

            'Apply border to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text with border")

            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.[Single]

            tr.CharacterFormat.Border.Color = Color.Black

            'Apply emphasis mark to text

            paragraph.AppendText(". This is ")

            tr = paragraph.AppendText("text with emphasis mark")

            tr.CharacterFormat.EmphasisMark = Emphasis.DotBelow

            'Apply superscript to text

            paragraph.AppendText(". This is a math formula: a")

            tr = paragraph.AppendText("2")

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript

            paragraph.AppendText(" + b")

            tr = paragraph.AppendText("2")

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript

            paragraph.AppendText(" = c")

            tr = paragraph.AppendText("2")

            tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript

            paragraph.AppendText(".")

            'Save to file

            document.SaveToFile("SetCharacterFormat.docx", FileFormat.Docx)

        End Sub

    End Class

End Namespace

Effective Shot

 Conclusion:

     In this post, you have learned how to align text in Word in C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET: Extract Text and Images from Word, C#/VB.NET: Find and Highlight Text in Word and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.


Comments

Popular posts from this blog

Java: How to encrypt or decrypt PDF documents?

Replace the existing image on the PDF file in C#/VB.NET

How to Convert PDF to Images in Java