Skip to main content
A newer version of this page is available. .

Paragraphs

  • 4 minutes to read

Access Paragraphs

The Paragraph class represents paragraphs in the RichEditDocumentServer.

The ParagraphCollection contains all document paragraphs. Access a specific paragraph by its index using the SubDocument.Paragraphs property. The Section.Paragraphs property provides access to a collection of paragraphs in a particular document section. You can also retrieve the paragraph related to the given range using the ReadOnlyParagraphCollection.Get method.

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;

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("FirstLook.docx");
    Document document = wordProcessor.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

Paragraphs can be formatted directly or using document styles. Refer to the Text Formatting topic for more examples on how to format paragraphs.

Note

The RichEditDocumentServer can load/save, print and export to PDF format documents with paragraph borders. The Word Processing Document API does not provide an API to manage paragraph borders.

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

Document document = server.Document;
document.BeginUpdate();
document.AppendText("Modified Paragraph\nNormal\nNormal");
document.EndUpdate();
DocumentPosition pos = document.Range.Start;
DocumentRange range = document.CreateRange(pos, 0);
ParagraphProperties pp = document.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 = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.5f);
// Set tab stop at 1.5"
TabInfoCollection tbiColl = pp.BeginUpdateTabs(true);
TabInfo tbi = new DevExpress.XtraRichEdit.API.Native.TabInfo();
tbi.Alignment = TabAlignmentType.Center;
tbi.Position = DevExpress.Office.Utils.Units.InchesToDocumentsF(1.5f);
tbiColl.Add(tbi);
pp.EndUpdateTabs(tbiColl);
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 to remove it from the document.

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

using DevExpress.XtraRichEdit.API.Native;

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("FirstLook.docx");
    Document document = wordProcessor.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();
}