Word documents are the favorite choice when it comes to creating reports and documenting content. Using bullets in a document enhances the readability of the document and helps in organizing the information. These also help in comparisons of data and highlight key information. As a .NET developer, you can provide the functionality of working with bullets from within your .NET application using C#/VB.NET.
In this blog, we’ll show how to work with bullets by creating bullets and sub-bullets in the Word document using NPOI API in C#.
How to Insert Multi-Level Bullets in a Document using Microsoft Word?
Before we can jump into writing code for creating multi-level bullets in a document, let’s have a look at how we can do the same using Microsoft Word. Microsoft Word lets you add bulleted content to your document. You can add headings and sub-headings as bullets and sub-bullets in the document.
Steps to Add Bullets to Document using Microsoft Word
You can add bulleted content to your document as shown in the steps below.
- Select the lines of text you want to convert to a bulleted list
- Clic Home> Paragraph> Bullets. Each line or paragraph becomes a bullet in the list.
How to Insert Multi-Level Bulleted List in Word using C#?
Now that we have seen how to insert a multi-level bulleted list in a DOCX file using Microsoft Word, we will now proceed towards doing the same using NPOI API for .NET in our C# application. If you haven’t already installed NPOI API, you can go through our comprehensive guide for NPOI API installation in your .NET project.
Step-by-Step Guide to Adding Multi-Level List in Word Document using C#
In order to create a bulleted list in a Word document using NPOI API in C#, use the following steps:
- Create an instance of XWPFDocument class
- Create an instance of XWPFNumbering class
- Add a paragraph using the instance of XWPFParagraph
- Create bullet lists by using the SetNumID method on the paragraph class instance
//Create document
XWPFDocument doc = new XWPFDocument();
//Create numbering
XWPFNumbering numbering = doc.CreateNumbering();
string abstractNumId = numbering.AddAbstractNum();
string numId = numbering.AddNum(abstractNumId);
//Create paragragh and set its list level
XWPFParagraph para1 = doc.CreateParagraph();
XWPFRun run1 = para1.CreateRun();
run1.SetText("Introduction to File Formats");
para1.SetNumID(numId, "0");
//Create paragragh and set the list level
XWPFParagraph para2 = doc.CreateParagraph();
XWPFRun run2 = para2.CreateRun();
run2.SetText("Popular File Formats and their Applications");
para2.SetNumID(numId, "0");
//Create paragragh and apply multi level list
XWPFParagraph para3 = doc.CreateParagraph();
XWPFRun run3 = para3.CreateRun();
run3.SetText("File Formats - Spreadsheet File Formats");
para3.SetNumID(numId, "0");
para3 = doc.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("XLSX");
para3.SetNumID(numId, "1");
para3 = doc.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("XLS");
para3.SetNumID(numId, "1");
para3 = doc.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("Converst XLS to XLSX");
para3.SetNumID(numId, "2");
//Save the file and Launch
using (FileStream sw = new FileStream("BullettedListUsingCSharp.doc", FileMode.Create))
{
doc.Write(sw);
}
Conclusion
In this article, we learned about how to insert bullets in a Word document using C# by utilizing NPOI API. NPOI API is an open-source free-to-use .NET API that lets you work with Word documents from within your .NET application. You can have a look at other NPOI examples for working with document files in C#. For more examples on working with NPOI , stay tuned.