officefileapi-15308-word-processing-document-api-word-processing-document-paragraphs.md
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 Paragraphs.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;
using (var wordProcessor = new RichEditDocumentServer()) {
Document document = wordProcessor.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);
}
Imports DevExpress.XtraRichEdit.API.Native
Using wordProcessor = New RichEditDocumentServer()
Dim document As Document = wordProcessor.Document
document.BeginUpdate()
' Append text to the document (multiple paragraphs).
document.AppendText("First Paragraph" & vbLf & "Second Paragraph" & vbLf & "Third Paragraph")
document.EndUpdate()
' Access the end position of the document range.
Dim position As DocumentPosition = document.Range.End
' Append text to the end of the last paragraph.
Dim doc As SubDocument = position.BeginUpdateDocument()
Dim par As Paragraph = doc.Paragraphs.Get(position)
Dim newPos As DocumentPosition = doc.CreatePosition(par.Range.End.ToInt() - 1)
doc.InsertText(newPos, "<<Appended to Paragraph End>>")
pos.EndUpdateDocument(doc)
End Using
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 snippet 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();
}
Imports DevExpress.XtraRichEdit.API.Native
Using wordProcessor = New RichEditDocumentServer()
wordProcessor.LoadDocument("FirstLook.docx")
Dim document As Document = wordProcessor.Document
' Start the document update:
document.BeginUpdate()
' Append a paragraph:
Dim appendedParagraph As Paragraph = document.Paragraphs.Append()
document.InsertText(appendedParagraph.Range.Start, "Appended paragraph")
' Insert a paragraph at the start of the second section:
Dim paragraph As Paragraph =
document.Paragraphs.Insert(document.Sections(1).Range.Start)
Dim position As DocumentPosition =
document.Paragraphs(paragraph.Index - 1).Range.Start
document.InsertText(position, "Inserted paragraph")
' Finalize the document update:
document.EndUpdate()
Format paragraphs directly or apply document styles. For more examples on formatting paragraphs, refer to this topic: Text Formatting
The following code snippet changes paragraph formatting in code:
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 document modification.
document.EndUpdate();
// Access the first paragraph's 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 tab stop modification.
pp.EndUpdateTabs(tbiColl);
// Finalize the paragraph edit operation.
document.EndUpdateParagraphs(pp);
}
Imports Microsoft.VisualBasic
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.Office.Utils
Using wordProcessor = New RicheditDocumentServer()
' Access a document.
Dim document As Document = wordProcessor.Document
' Start to edit the document.
document.BeginUpdate()
' Append text to the document.
document.AppendText("Modified Paragraph\nNormal\nNormal")
' Finalize document modification.
document.EndUpdate()
' Access the first paragraph range
Dim range As DocumentRange = document.Paragraphs(0).Range
' Start to edit the paragraph.
Dim pp As ParagraphProperties = 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.
Dim tbiColl As TabInfoCollection = pp.BeginUpdateTabs(True)
' Create a new tab stop for the paragraph.
Dim tbi As TabInfo = 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 tab stop modification.
pp.EndUpdateTabs(tbiColl)
' Finalize the paragraph edit operation.
document.EndUpdateParagraphs(pp)
End Using
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;
using (var wordProcessor = new RichEditDocumentServer()) {
Document document = wordProcessor.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;
}
Imports Microsoft.VisualBasic
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Drawing
Using wordProcessor = New RichEditDocumentServer()
Dim document As Document = wordProcessor.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 the range from the first to the last paragraph
Dim firstParagraph As Paragraph = document.Paragraphs(0)
Dim thirdParagraph As Paragraph = document.Paragraphs(2)
Dim paragraphRange As DocumentRange = document.CreateRange(first.Range.Start, third.Range.End.ToInt() - firstParagraph.Range.Start.ToInt())
' Start to edit the paragraph.
Dim pp As ParagraphProperties = 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)
End Using
Shared Sub SetBorder(ByVal border As ParagraphBorder)
border.LineWidth = 2F
border.LineStyle = BorderLineStyle.Thick
border.LineColor = Color.SteelBlue
End Sub
Call the SubDocument.Delete method and pass the paragraph range to remove it from the document.
The following code snippet locates and removes paragraphs at the end of the first section:
using DevExpress.XtraRichEdit.API.Native;
using System.Linq;
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("FirstLook.docx");
Document document = wordProcessor.Document;
document.BeginUpdate();
// Retrieve the 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();
}
Imports DevExpress.XtraRichEdit.API.Native
Import System.Linq
Using wordProcessor As New RichEditDocumentServer()
wordProcessor.LoadDocument("FirstLook.docx")
Dim document As Document = wordProcessor.Document
document.BeginUpdate()
' Retrieve the first section
Dim firstSection As Section = document.Sections(0)
' Calculate the length of the last two paragraphs in section
Dim lastParagraph As Paragraph = firstSection.Paragraphs.Last()
Dim secondLastParagraph As Paragraph = firstSection.Paragraphs(lastParagraph.Index - 1)
Dim length As Integer = secondLastParagraph.Range.Length + lastParagraph.Range.Length
' Define a range to delete
Dim deleteRange As DocumentRange =
document.CreateRange(secondLastParagraph.Range.Start, length)
' Clear the range
document.Delete(deleteRange)
document.EndUpdate()
End Using