Microsoft Word is to stay here and remain one of the widely used document editors for creating documents. Almost everyone knows about it and uses it in daily life one way or the other. People from every aspect of life use it one way or the other to generate documents. You must be familiar with the .docx file format which is the default file format for saving Word documents.

Being a .NET application developer, it is common to provide document processing capability to your .NET application. You can achieve this using NPOI API in your application using C# or VB.NET as shown in this article.

Format Text Using Microsoft Word

Before we jump into writing a .NET application to format text in a Word document, let’s have a look at how we can do the same using Microsoft Word.

Steps to Format Text with Microsoft Word

In Microsoft Word, you can format text in a number of ways. You can make text bold, format it to appear as Italic, underline it, make it strikethrough, apply color to text, and many other types of formatting. In order to set the style of your text, you can use the following steps.

  1. Open Microsoft Document and select Blank Document. This will open a blank document for you to write something.
  2. Now add some text to the document
  3. Select the text and use the B option from Ribbon to make it bold
  4. Select the I option from Ribbon to make it Italic
  5. You can also select U option to make the text underline
  6. You can also change the color of the text by using the Text Color option from Ribbon

These options are shown in the image below.

Text Formatting in Word Document using NPOI in C#

Now that we have seen how to format text in a document using Microsoft Word, we are ready to write our .NET application for doing the same. We’ll be using NPOI API for doing the same and developing a console-based application in C#. Just in case you still haven’t installed NPOI API in your .NET project, you can have a look at this instructions guide for Installing NPOI for .NET.

Steps to Format Text in DOCX in C#

At this age, we hope your development environment is ready and you are ready to start writing your code for your application to format text in a Word document using C#. You can use the following source code for this purpose.

//Create document
XWPFDocument document = new XWPFDocument();

//Create paragraph
XWPFParagraph para1 = document.CreateParagraph();
para1.Alignment = ParagraphAlignment.CENTER;

//Set style of the paragraph text
XWPFRun run1 = para1.CreateRun();
run1.FontSize = 18;
run1.SetText("This is the first paragraph");
run1.IsBold = true;

XWPFParagraph para2 = document.CreateParagraph();
para2.Alignment = ParagraphAlignment.LEFT;
para2.BorderTop = Borders.Gems;

XWPFRun run2 = para2.CreateRun();
run2.Subscript = VerticalAlign.BASELINE;
run2.IsItalic = true;
run2.Underline = UnderlinePatterns.Dash;
run2.SetText("This is the second paragraph");

//Save the file
using (FileStream file = File.Create("TextStyleFormattingUsingNPOI.docx"))


NPOI is a powerful API for working with Office file formats. You can use it to develop .NET applications for working with Word documents right from within your application. For more examples of working with Word documents using NPOI in C#, stay tuned.