How to: Remove Blank Lines from a Document

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

document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
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));