Back to Devexpress

How to: Change Formatting of the Current Paragraph

windowsforms-8173-controls-and-libraries-rich-text-editor-examples-formatting-how-to-change-formatting-of-the-current-paragraph.md

latest3.9 KB
Original Source

How to: Change Formatting of the Current Paragraph

  • Apr 14, 2022
  • 2 minutes to read

This code sample demonstrates how you can modify the attributes of the current paragraph. The Document.Selection property is used to obtain the DocumentRange object, representing the user selection.

To modify paragraph formatting, such as the ParagraphPropertiesBase.LineSpacing, ParagraphPropertiesBase.Alignment, ParagraphPropertiesBase.SpacingBefore etc., call the SubDocument.BeginUpdateParagraphs method for the specified range, modify the properties of the returned ParagraphProperties object and call the SubDocument.EndUpdateParagraphs method to finalize the modification.

The following code snippet changes the first line indent and the line spacing of the paragraph containing the selection.

Note

To change default paragraph formatting for the current document, use the Document.DefaultParagraphProperties property.

View Example: WinForms RichEdit Document API

csharp
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.Office.Utils;

document.BeginUpdate();
document.AppendText("Modified Paragraph\nNormal\nNormal");
document.EndUpdate();

// Obtain the first paragraph range
DocumentRange range = document.Paragraphs[0].Range;

// Obtain paragraph options
ParagraphProperties pp = document.BeginUpdateParagraphs(range);

// Center a paragraph
pp.Alignment = ParagraphAlignment.Center;

// Set triple spacing
pp.LineSpacingType = ParagraphLineSpacing.Multiple;
pp.LineSpacingMultiplier = 3;

// Set left indent at 0.5".
// Default unit is 1/300 of an inch (a document unit).
pp.LeftIndent = Units.InchesToDocumentsF(0.5f);

// Set a tab stop at 1.5"
TabInfoCollection tbiColl = pp.BeginUpdateTabs(true);
TabInfo tbi = new TabInfo();
tbi.Alignment = TabAlignmentType.Center;
tbi.Position = Units.InchesToDocumentsF(1.5f);
tbiColl.Add(tbi);
pp.EndUpdateTabs(tbiColl);

//Finalize the update
document.EndUpdateParagraphs(pp);
vb
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.Office.Utils

document.BeginUpdate()
document.AppendText("Modified Paragraph" & vbLf & "Normal" & vbLf & "Normal")
document.EndUpdate()

' Obtain the first paragraph range
Dim pos As DocumentPosition = document.Range.Start
Dim range As DocumentRange = document.CreateRange(pos, 0)

' Obtain paragraph options
Dim pp As ParagraphProperties = document.BeginUpdateParagraphs(range)

' Center a paragraph
pp.Alignment = ParagraphAlignment.Center

' Set triple spacing
pp.LineSpacingType = ParagraphLineSpacing.Multiple
pp.LineSpacingMultiplier = 3

' Set left indent at 0.5".

' Default unit is 1/300 of an inch (a document unit).
pp.LeftIndent = Units.InchesToDocumentsF(0.5F)

' Set a tab stop at 1.5"
Dim tbiColl As TabInfoCollection = pp.BeginUpdateTabs(True)
Dim tbi As TabInfo = New TabInfo()
tbi.Alignment = TabAlignmentType.Center
tbi.Position = Units.InchesToDocumentsF(1.5F)
tbiColl.Add(tbi)
pp.EndUpdateTabs(tbiColl)

'Finalize the update
document.EndUpdateParagraphs(pp)