Back to Devexpress

How to: Remove Blank Lines from a Document

windowsforms-10445-controls-and-libraries-rich-text-editor-examples-search-and-replace-how-to-remove-blank-lines-from-a-document.md

latest1.5 KB
Original Source

How to: Remove Blank Lines from a Document

  • Feb 24, 2025

To delete excessive blank lines from a loaded document, execute the SubDocument.ReplaceAll with the appropriate regular expression as the search parameter. Then save the resulting document.

The following code snippet removes repetitive blank lines.

View Example

csharp
document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.Docx);
string pattern = @"((?<=^)|(?<=\n))\n";
string replacementString = string.Empty;
System.Text.RegularExpressions.Regex myRegEx = 
    new System.Text.RegularExpressions.Regex(pattern);
int count = document.ReplaceAll(myRegEx, replacementString);
System.Windows.Forms.MessageBox.Show(String.Format("{0} blank lines have been removed",count));
vb
document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.Docx)
Dim pattern As String = "((?<=^)|(?<=\n))\n"
Dim replacementString As String = String.Empty
Dim myRegEx As New System.Text.RegularExpressions.Regex(pattern)
Dim count As Integer = document.ReplaceAll(myRegEx, replacementString)
System.Windows.Forms.MessageBox.Show(String.Format("{0} blank lines have been removed",count))