Convert Plain Text to Bulleted Lists in C# with .NET
This article shows how to convert plain text to bulleted lists in C# with .NET. It parses the paragraphs in the current selection and converts them to a bulleted list by recognizing the leading characters of each paragraph.

Better user experience and productivity often result from simplicity and automation. You can programmatically convert plain text lists into actual bulleted lists using the powerful API of TX Text Control. This feature not only improves the appearance of documents, but also makes them more structured and easier to read. Let's explore why this approach is so valuable and how it works with a code example.
During document creation, users often create content in plain text editors or import text from external sources. Plain text lists-those that are preceded by characters such as dashes - or asterisks * are a common way to organize information. However, these lists lack the proper formatting and structure of true bulleted lists. Once converted, lists can be easily modified using TX Text Control features. For example, you can change bullet styles or adjust indentation levels.
Converting Plain Text Lists
TX Text Control provides a robust API that allows developers to recognize and convert plain text lists into structured bulleted lists. This includes text lines that begin with common list characters such as -, *, or +. Indented lists created with tabs are also supported, allowing seamless conversion of hierarchical list structures.
The following method converts the currently selected text in a TX Text Control document into a bulleted list.
void ApplyBulletedListFormatting(TextControl textControl, int baseLeftMargin)
{
// Define the set of valid bullet characters
var bulletChars = new HashSet<char> { '•', 'o', 'O', '0', '-', '-', '-', '*', '+' };
TXTextControl.Selection selection = textControl.Selection;
int offset = 0;
// Iterate through all paragraphs in the TextControl
foreach (Paragraph paragraph in textControl1.Paragraphs)
{
// Check if the paragraph is within the selection
if (paragraph.Start < selection.Start - offset || paragraph.Start > selection.Start + selection.Length - offset)
continue;
string text = paragraph.Text;
int textLength = text.Length;
if (textLength > 1)
{
int tabCount = 0;
// Count the number of leading tab characters
while (tabCount < textLength && text[tabCount] == '\t')
{
tabCount++;
}
// Check if there's a bullet character followed by a space
if (tabCount < textLength - 1 &&
bulletChars.Contains(text[tabCount]) &&
text[tabCount + 1] == ' ')
{
textControl1.Selection.Length = textControl1.Selection.Length - 1;
// Set list type to bulleted
paragraph.ListFormat.Type = ListType.Bulleted;
// Remove the leading tabs and bullet character
textControl1.Select(paragraph.Start - 1, tabCount + 2);
textControl1.Selection.Text = string.Empty;
// Adjust the selection offset
offset += tabCount + 2;
// Configure list level and indentation
paragraph.ListFormat.Level = tabCount + 1;
paragraph.ListFormat.LeftIndent = tabCount > 0 ? baseLeftMargin * tabCount : 0;
}
}
}
}
Step-by-Step Process
TX Text Control is used to loop through the paragraphs in the selected area of text. Then it checks the depth of the list by counting the number of tabs that follow, and checking to see if there is a leading bullet followed by a space.
- First item
- Second item
- Sub-item 1
- Sub-item 2
The paragraph is then converted to a bulleted list with the appropriate level and indent, and the leading text characters are removed.
Conclusion
Converting plain text lists to bulleted lists is a powerful feature that improves the appearance and structure of documents. This approach is especially useful when importing content from external sources or when users create content in plain text editors. By automating the conversion process, you can improve the user experience and productivity of your applications.
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
Extracting Comments from DOCX Files in .NET C#
This article demonstrates how to extract comments from DOCX files using TX Text Control .NET Server. The sample code extracts all comments from a DOCX file and uses lambda expressions to filter…
Why HTML is not a Substitute for Page-Oriented Formats like DOCX
In this blog post, we will discuss the limitations of HTML as a document format and explain why page-oriented formats, such as DOCX, remain essential for certain use cases. We will explore the…
Convert MS Word DOCX to PDF including Text Reflow using .NET C# on Linux
This article explains how to use TX Text Control .NET Server to convert a Microsoft Word DOCX document to a PDF file on a Linux system using .NET C#. This conversion process includes text reflow,…
Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…
This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…
How to Import and Read Form Fields from DOCX Documents in .NET on Linux
Learn how to import and read form fields from DOCX documents in .NET on Linux using TX Text Control. This article provides a step-by-step guide to help you get started with form fields in TX Text…