Paragraphs in Rich Text Documents
- 6 minutes to read
The Paragraph class contains paragraph parameters in the RichEditControl. You can manage them in code and using the User Interface.
Paragraphs in the User Interface
Users can press Enter to divide text into paragraphs, and the Paragraph group on the Home ribbon tab allows them to format paragraphs. Refer to the following topic for information on how to add a ribbon UI for the RichEditControl: How to: Create a Simple Word Processor with a Ribbon UI
Users can specify advanced paragraph settings in the Paragraph Dialog. You can invoke this dialog from the ribbon group of from the context menu.
Tip
You can restrict users from formatting paragraphs or creating new paragraphs. Set the following properties to Hidden or Disabled to disable or hide the corresponding commands in the Ribbon UI and the pop-up menu:
The RichEditControl can display, print, and export to PDF format documents with paragraph borders.
The control does not contain user interface elements used to create or modify paragraph borders.
Paragraphs in Code
Access Paragraphs
The ParagraphCollection holds all document paragraphs. Access specific paragraphs by their index with the SubDocument.Paragraphs property. The Section.Paragraphs property allows you to retrieve paragraphs within a specific document section. Alternatively, retrieve paragraphs for a specified range by the ReadOnlyParagraphCollection.Get method call.
The code snippet retrieves the paragraph related to the specified position and appends text to the end of this paragraph:
using DevExpress.XtraRichEdit.API.Native;
Document document = richEditControl.Document;
document.BeginUpdate();
// Append text to the document (multiple paragraphs).
document.AppendText("First Paragraph\nSecond Paragraph\nThird Paragraph");
document.EndUpdate();
// Access the end position of the document range.
DocumentPosition position = document.Range.End;
// Append text to the end of the last paragraph.
SubDocument doc = position.BeginUpdateDocument();
Paragraph par = doc.Paragraphs.Get(position);
DocumentPosition newPos = doc.CreatePosition(par.Range.End.ToInt() - 1);
doc.InsertText(newPos, "<<Appended to Paragraph End>>");
pos.EndUpdateDocument(doc);
Add New Paragraph
To insert a new paragraph, use one of these methods:
Method | Description |
---|---|
ParagraphCollection.Append | Appends a new paragraph. |
ParagraphCollection.Insert | Inserts a new paragraph at the specified document position and returns the paragraph that follows the inserted paragraph. |
The following code snippet 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 paragraph:
Paragraph appendedParagraph = document.Paragraphs.Append();
document.InsertText(appendedParagraph.Range.Start, "Appended paragraph");
//Insert 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
Format paragraphs directly or apply document styles. For more examples on formatting paragraphs, refer to this topic: Text Formatting.
The following snippet changes paragraph formatting in code:
using System.Windows;
using System.Drawing;
using DevExpress.XtraRichEdit;
using DevExpress.Office.Utils;
using DevExpress.XtraRichEdit.API.Native;
//...
Document doc = richEditControl1.Document;
DocumentRange range = doc.Selection;
ParagraphProperties pp = doc.BeginUpdateParagraphs(range);
// Center 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 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);
doc.EndUpdateParagraphs(pp);
Set Paragraph Borders
Obtain border settings for a specific range through the ParagraphProperties.Borders property.
The Paragraph.Borders property allows you to specify borders for an individual paragraph. Call the ParagraphBorders.Reset() method to reset borders.
The following code snippet sets borders for multiple paragraphs:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
Document document = richEditControl.Document;
// Start to edit the document.
document.BeginUpdate();
// Append text to the document.
document.AppendText(String.Format("Modified Paragraph" +
Environment.NewLine + "Normal" + Environment.NewLine + "Normal"));
// Finalize the edit operation.
document.EndUpdate();
// Obtain a range from the first to the last paragraph
Paragraph firstParagraph = document.Paragraphs[0];
Paragraph thirdParagraph = document.Paragraphs[2];
DocumentRange paragraphRange =
document.CreateRange(firstParagraph.Range.Start, thirdParagraph.Range.End.ToInt() - firstParagraph.Range.Start.ToInt());
// Start to edit the paragraph.
ParagraphProperties pp = document.BeginUpdateParagraphs(paragraphRange);
SetBorder(pp.Borders.HorizontalBorder);
SetBorder(pp.Borders.BottomBorder);
SetBorder(pp.Borders.TopBorder);
SetBorder(pp.Borders.LeftBorder);
SetBorder(pp.Borders.RightBorder);
// Finalize the edit operation.
document.EndUpdateParagraphs(pp);
static void SetBorder(ParagraphBorder border)
{
border.LineWidth = 2f;
border.LineStyle = BorderLineStyle.Thick;
border.LineColor = Color.SteelBlue;
}
Remove Paragraphs
Call the SubDocument.Delete method and pass the paragraph range to remove it from the document.
The following code snippet locates paragraphs at the end of the first section and removes them:
using DevExpress.XtraRichEdit.API.Native;
richEditControl.LoadDocument("FirstLook.docx");
Document document = richEditControl.Document;
document.BeginUpdate();
//Retrieve first section:
Section firstSection = document.Sections[0];
//Calculate the length of the last two paragraphs in section:
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();