Skip to main content

Paragraphs in Rich Text Documents

  • 5 minutes to read

Paragraphs in the User Interface

Users can press Enter to divide text into paragraphs. The Paragraph group on the Home ribbon tab allows you to format paragraphs. Refer to the How to: Create the RichEditControl with a Ribbon UI lesson for information on how to create a RichEditControl application with a Ribbon UI.

RichEdit_Ribbon_Paragraph

Use the Paragraph Dialog to specify advanced paragraph settings. You can invoke this dialog from the ribbon group or from the context menu.

RichEdit_ShowParagraphDialog_RibbonCaptionButton

RTEParagraphContextMenu

Tip

You can restrict users from formatting paragraphs or creating new paragraphs. Set the DocumentCapabilitiesOptions.Paragraphs, ParagraphFormatting, or ParagraphTabs property to DocumentCapability.Hidden or DocumentCapability.Disabled to disable or hide the corresponding commands in the command UI and the pop-up menu.

The RichEditControl can display, print and export to PDF format documents with paragraph borders.

XtraRichEdit_ParagraphBorders

Note

The control does not provide user interface elements to create or modify paragraph borders.

Paragraphs in Code

Access Paragraphs

The ParagraphCollection contains all document paragraphs. Use the SubDocument.Paragraphs property to access a specific paragraph by its index. The Section.Paragraphs property obtains paragraphs in a specific document section. You can also call the ReadOnlyParagraphCollection.Get method to retrieve the paragraph related to the specified range.

Add New Paragraph

Use one of the following methods to insert new paragraph:

Method Description
ParagraphCollection.Append Appends new paragraph.
ParagraphCollection.Insert Inserts new paragraph at the specified document position. Returns a paragraph that follows the inserted paragraph.

The code sample below appends a paragraph and inserts a paragraph at the start of the second section:

using DevExpress.XtraRichEdit.API.Native;

richEditControl.LoadDocument("FirstLook.docx");
Document document = richEditControl.Document;
document.BeginUpdate();

// Append a  paragraph:
Paragraph appendedParagraph = document.Paragraphs.Append();
document.InsertText(appendedParagraph.Range.Start, "Appended paragraph");

// Insert a new paragraph at the start of the second section:
Paragraph paragraph = document.Paragraphs.Insert(document.Sections[1].Range.Start);
document.InsertText(document.Paragraphs[paragraph.Index - 1].Range.Start,
    "Inserted paragraph");
document.EndUpdate();

Format Paragraphs

Paragraphs can be formatted directly or by document styles. Refer to the following topic for more examples on how to format paragraphs:

Read Tutorial: Text Formatting

Note

The Rich Text Editor can load/save, print and export to PDF format documents with paragraph borders. There is no API to manage paragraph borders.

The code sample below shows how to change paragraph formatting in code:

View Example: WinForms RichEdit Document API

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);

Note

We recommend to format paragraphs using ParagraphProperties options instead of specifying the formatting properties directly to the Paragraph object. Use the Paragraph object’s properties to check the information.

Remove Paragraphs

Call the SubDocument.Delete method and pass the paragraph range as the method parameter to remove the paragraph from the document.

The code sample below obtains and removes paragraphs at the end of the first section:

using DevExpress.XtraRichEdit.API.Native;
using System.Linq;

richEditControl.LoadDocument("FirstLook.docx");
Document document = richEditControl.Document;
document.BeginUpdate();

// Retrieve the first section:
Section firstSection = document.Sections[0];

// Calculate the length of the last two paragraphs:
Paragraph lastParagraph = firstSection.Paragraphs.Last();
Paragraph secondLastParagraph =
         firstSection.Paragraphs[lastParagraph.Index - 1];
int length = secondLastParagraph.Range.Length + lastParagraph.Range.Length;

// Define a range to delete:
DocumentRange deleteRange =
    document.CreateRange(secondLastParagraph.Range.Start, length);

// Clear the range:
document.Delete(deleteRange);

document.EndUpdate();
See Also