Skip to main content

Paragraphs in Word Documents

  • 5 minutes to read

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;

using (var wordProcessor = new RichEditDocumentServer())
{
  wordProcessor.LoadDocument("FirstLook.docx");
  Document document = wordProcessor.Document;

  // Start the document update:
  document.BeginUpdate();

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

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

  // Finalize the document update:
  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 RichEditDocumentServer can load/save, print and export to PDF format documents with paragraph borders. The Word Processing Document API does not ship with an API to manage paragraph borders.

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

result

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

using (var wordProcessor = new RicheditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;

    // Start to edit the document.
    document.BeginUpdate();

    // Append text to the document.
    document.AppendText("Modified Paragraph\nNormal\nNormal");

    // Finalize to edit the document.
    document.EndUpdate();

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

    // Start to edit the paragraph.
    ParagraphProperties pp = document.BeginUpdateParagraphs(range);

    // Specify the paragraph's alignment.
    pp.Alignment = ParagraphAlignment.Center;

    // Specify the paragraph's line spacing.
    pp.LineSpacingType = ParagraphLineSpacing.Multiple;
    pp.LineSpacingMultiplier = 3;

    // Set the paragraph's left indent to 0.5 document unit.
    // Default unit is 1/300 of an inch (a document unit).
    pp.LeftIndent = Units.InchesToDocumentsF(0.5f);

    // Start to modify tab stops in the paragraph.
    TabInfoCollection tbiColl = pp.BeginUpdateTabs(true);

    // Create a new tab stop for the paragraph.
    TabInfo tbi = TabInfo();

    // Specify the tab stop's alignment type.
    tbi.Alignment = TabAlignmentType.Center;

    // Set the tab stop position to 1.5 document unit.
    tbi.Position = Units.InchesToDocumentsF(1.5f);

    // Add the tab stop to the collection of tab stops.
    tbiColl.Add(tbi);

    // Finalize to modify tab stops in the paragraph.
    pp.EndUpdateTabs(tbiColl);

    // Finalize to edit the paragraph.
    document.EndUpdateParagraphs(pp);
}

Note

We recommend that you use the 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 the paragraph 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 System.Linq;

using (var 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();
}