Sample Code: Removing All Empty Paragraphs Completely
A common request from our users is how to remove empty paragraphs from a document. Not only the text, but the whole paragraph including the end of paragraph character. The following function uses the ParagraphEnumerator to iterate through all paragraphs. If the trimmed text of a paragraph is empty, we can remove the whole paragraph using the Selection class. private void RemoveEmptyPararaphs(TXTextControl.ParagraphCollection pars) { TXTextControl.ParagraphCollection.ParagraphEnumerator…

A common request from our users is how to remove empty paragraphs from a document. Not only the text, but the whole paragraph including the end of paragraph character. The following function uses the ParagraphEnumerator to iterate through all paragraphs. If the trimmed text of a paragraph is empty, we can remove the whole paragraph using the Selection class.
private void RemoveEmptyPararaphs(TXTextControl.ParagraphCollection pars)
{
TXTextControl.ParagraphCollection.ParagraphEnumerator enumParagraphs =
textControl1.Paragraphs.GetEnumerator();
int iParCounter = textControl1.Paragraphs.Count;
enumParagraphs.MoveNext();
for(int i = 0; i < iParCounter; i++)
{
TXTextControl.Paragraph par =
(TXTextControl.Paragraph)enumParagraphs.Current;
if (string.IsNullOrEmpty(par.Text.Trim()) == true)
{
par.Select();
textControl1.Selection.Length += 1;
textControl1.Selection.Text = "";
}
else
enumParagraphs.MoveNext();
}
}
Related Posts
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
ASP.NETJavaScriptDocument Editor
Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More
This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.