Back to Devexpress

Document.InsertSection(DocumentPosition) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-document-dot-insertsection-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentposition-x29.md

latest7.6 KB
Original Source

Document.InsertSection(DocumentPosition) Method

Inserts a new section into the document’s Document.Sections collection at a specified position.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
Section InsertSection(
    DocumentPosition pos
)
vb
Function InsertSection(
    pos As DocumentPosition
) As Section

Parameters

NameTypeDescription
posDocumentPosition

A DocumentPosition object that specifies the position into which you can insert a new section.

|

Returns

TypeDescription
Section

A Section object that is the section in the document located before the newly inserted section.

|

Remarks

When you create a new section, it is automatically linked to the previous section and content from that section’s header is automatically inserted into the second section header. Remove this content before inserting new content in the header. Call the SubDocument.Delete() method to clear the header.

Example

The following code sample shows how to insert a continuous section break after the specified paragraph:

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument(@"Documents//Alice.docx");

    Document document = wordProcessor.Document;
    var section = document.InsertSection(document.Paragraphs[4].Range.End);

    // Retrieve the inserted section
    var sectionPosition = document.CreatePosition(section.Range.End.ToInt() + 1);
    Section insertedSection = document.GetSection(sectionPosition);
    insertedSection.StartType = SectionStartType.Continuous;

    wordProcessor.SaveDocument("Alice_formatted.docx", DocumentFormat.Docx);   
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
    wordProcessor.LoadDocument("Documents//Alice.docx")

    Dim document As Document = wordProcessor.Document
    Dim section = document.InsertSection(document.Paragraphs(4).Range.End)

    ' Retrieve the inserted section
    Dim sectionPosition = document.CreatePosition(section.Range.End.ToInt() + 1)
    Dim insertedSection As Section = document.GetSection(sectionPosition)
    insertedSection.StartType = SectionStartType.Continuous

    wordProcessor.SaveDocument("Alice_formatted.docx", DocumentFormat.Docx)
End Using

The following code snippets (auto-collected from DevExpress Examples) contain references to the InsertSection(DocumentPosition) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-table-of-contents-practical-guide/CS/Form1.cs#L98

csharp
Document.EndUpdateCharacters(cp);
Document.InsertSection(field.Range.End);
field.Update();

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/SearchAndReplace.cs#L13

csharp
document.LoadDocument("Documents//Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
document.InsertSection(document.Range.Start);
// Specify a regular expression that will find all six letter words.

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/SearchAndReplaceActions.cs#L13

csharp
document.LoadDocument("Documents//Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
document.InsertSection(document.Range.Start);
// Specify a regular expression that will find all six letter words.

word-processing-table-of-contents-practical-guide/CS/Program.cs#L130

csharp
document.EndUpdateCharacters(cp);
document.InsertSection(field.Range.End);
field.Update();

winforms-richedit-table-of-contents-practical-guide/VB/Form1.vb#L90

vb
Document.EndUpdateCharacters(cp)
Document.InsertSection(field.Range.End)
field.Update()

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/SearchAndReplace.vb#L12

vb
document.LoadDocument("Documents//Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
document.InsertSection(document.Range.Start)
' Specify a regular expression that will find all six letter words.

wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/SearchAndReplaceActions.vb#L10

vb
document.LoadDocument("Documents//Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
document.InsertSection(document.Range.Start)
' Specify a regular expression that will find all six letter words.

word-processing-table-of-contents-practical-guide/VB/Program.vb#L127

vb
document.EndUpdateCharacters(cp)
document.InsertSection(field.Range.End)
field.Update()

See Also

Insert

Document Interface

Document Members

DevExpress.XtraRichEdit.API.Native Namespace