Skip to main content

How To: Add Line Numbering in the Rich Text Editor

  • 2 minutes to read

The following example illustrates how to specify the RichEdit View and section settings to enable line numbering.

Specify View Parameters

In SimpleView and DraftView, line numbers are located outside the visible area. Set the left padding to a higher value to add space for line numbers. To do that, use the SimpleView.Padding or the DraftView.Padding properties. To enable line numbering, set the RichEditView.AllowDisplayLineNumbers option to true.

using DevExpress.Portable;
using DevExpress.XtraRichEdit;

// Specify Simple View parameters:
SimpleView simpleView = richEditControl.Views.SimpleView;
simpleView.AllowDisplayLineNumbers = true;
simpleView.Padding = new PortablePadding(60, 0, 0, 0);

// Specify Draft View parameters:
DraftView draftView = richEditControl.Views.DraftView;
draftView.AllowDisplayLineNumbers = true;
draftView.Padding = new PortablePadding(60, 0, 0, 0);

Tip

The PrintLayoutView.AllowDisplayLineNumbers property’s default value is true, so you don’t need to adjust view parameters to enable line numbering.

Specify Section Parameters

Use the SectionLineNumbering class properties to specify line numbering parameters for a specific section. The SectionLineNumbering.CountBy property is mandatory.

The Section.LineNumbering property obtains the SectionLineNumbering instance.

The following code snippet enables line numbering for the first section in the loaded document. Line numbering starts at number one and restarts at the new section. Numbers are displayed on each second line and are indented from the text at a distance equal to 0.1 of an inch.

line numbering

View Example

using DevExpress.Office;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

// Change document measure units
// to calculate the line number distance correctly
richEditControl.Document.Unit = DocumentUnit.Inch;

richEditControl.LoadDocument("..\\..\\FirstLook.docx");
richEditControl.DocumentLoaded += RichEditControl_DocumentLoaded;

private void RichEditControl_DocumentLoaded(object sender, System.EventArgs e) {
    Section section = richEditControl.Document.Sections[0];
    SectionLineNumbering lineNumbering = section.LineNumbering;
    lineNumbering.CountBy = 2;
    lineNumbering.Start = 1;
    lineNumbering.Distance = 0.1f;
    lineNumbering.RestartType = LineNumberingRestart.NewSection;
}