officefileapi-120432-word-processing-document-api-examples-search-and-replace-how-to-remove-blank-lines-from-a-document.md
This code example shows how to retrieve excessive blank lines from a document. You can remove them or replace them with line break symbols.
Call the SubDocument.ReplaceAll method with the corresponding regular expression as the search parameter. Then save the result.
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Text.RegularExpressions;
using (var wordProcessor = new RichEditDocumentServer()) {
Document document = wordProcessor.Document;
document.LoadDocument("Grimm.docx");
string pattern = @"((?<=^)|(?<=\n))\n";
string replacementString = string.Empty;
Regex myRegEx = new Regex(pattern);
document.ReplaceAll(myRegEx, replacementString);
}
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Text.RegularExpressions
Using wordProcessor = New RichEditDocumentServer()
Dim document As Document = wordProcessor.Document
document.LoadDocument("Grimm.docx")
Dim pattern As String = "((?<=^)|(?<=\n))\n"
Dim replacementString As String = String.Empty
Dim myRegEx As New Regex(pattern)
document.ReplaceAll(myRegEx, replacementString)
End Using
Use the Characters enumeration to replace blank lines with line breaks. Call the SubDocument.ReplaceAll and pass the Characters.LineBreak enumeration value as a method parameter. You can use the “((?<=^)|(?<=\n))\n” expression to find blank lines.
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Text.RegularExpressions;
using DevExpress.Office;
using (var wordProcessor = new RichEditDocumentServer())
{
Document document = wordProcessor.Document;
document.LoadDocument(@"C:\Docs\Word (RTF) Document API for NET.docx");
string pattern = @"((?<=^)|(?<=\n))\n";
Regex myRegEx = new Regex(pattern);
document.ReplaceAll(myRegEx, Characters.LineBreak.ToString());
wordProcessor.SaveDocument("updated.docx", DocumentFormat.Docx);
}
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Text.RegularExpressions
Imports DevExpress.Office
Using wordProcessor = New RichEditDocumentServer()
Dim document As Document = wordProcessor.Document
document.LoadDocument("C:\Docs\Word (RTF) Document API for NET.docx")
Dim pattern As String = "((?<=^)|(?<=\n))\n"
Dim myRegEx As New Regex(pattern)
document.ReplaceAll(myRegEx, Characters.LineBreak.ToString())
wordProcessor.SaveDocument("updated.docx", DocumentFormat.Docx)
End Using