Word font and color in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to set font and color.

//Create word document
Document document = new Document();

//Create a new secition
Section section = document.AddSection();

//Create a new paragraph
Paragraph paragraph = section.AddParagraph();

//Append Text
String text
    = "This paragraph is demo of text font and color. "
    + "The font name of this paragraph is Tahoma. "
    + "The font size of this paragraph is 20. "
    + "The under line style of this paragraph is DotDot. "
    + "The color of this paragraph is Blue. ";
TextRange txtRange = paragraph.AppendText(text);

//Font name
txtRange.CharacterFormat.FontName = "Tahoma";

//Font size
txtRange.CharacterFormat.FontSize = 20;

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

//Change text color
txtRange.CharacterFormat.TextColor = Color.Blue;

//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);

'Create word document
Dim document_Renamed As New Document()

'Create a new secition
Dim section_Renamed As Section = document_Renamed.AddSection()

'Create a new paragraph
Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()

      'Append Text
      Dim text As String _
          = "This paragraph is demo of text font and color. " _
          & "The font name of this paragraph is Tahoma. " _
          & "The font size of this paragraph is 20. " _
          & "The under line style of this paragraph is DotDot. " _
          & "The color of this paragraph is Blue. "
      Dim txtRange As TextRange = paragraph_Renamed.AppendText(text)

      'Font name
      txtRange.CharacterFormat.FontName = "Tahoma"

      'Font size
      txtRange.CharacterFormat.FontSize = 20

      'Underline
      txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot

      'Change text color
      txtRange.CharacterFormat.TextColor = Color.Blue

'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)