Back to Devexpress

Split Documents

officefileapi-120385-word-processing-document-api-merge-and-split-documents-split-documents.md

latest10.2 KB
Original Source

Split Documents

  • Apr 23, 2025
  • 4 minutes to read

The RichEditDocumentServer allows you to split documents into separate files. Follow the steps below to complete the task:

Obtain a Specific Document Part

You can use the following approaches to get a specific document part:

Section

Use the Section.Range property to access the required section’s range.

Page

Call the DocumentLayout.GetPage method to iterate through the document pages. The LayoutPage.MainContentRange property returns the obtained page’s range as a FixedRange object. Call the SubDocument.CreateRange method to convert the retrieved FixedRange to DocumentRange.

csharp
int pageCount = server.DocumentLayout.GetPageCount();
for (int i = 0; i < pageCount; i++)
{
    LayoutPage layoutPage = server.DocumentLayout.GetPage(i);
    DocumentRange mainBodyRange = sourceServer.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length);
}
vb
Private Sub SurroundingSub()
    Dim pageCount As Integer = server.DocumentLayout.GetPageCount()

    For i As Integer = 0 To pageCount - 1
        Dim layoutPage As LayoutPage = server.DocumentLayout.GetPage(i)
        Dim mainBodyRange As DocumentRange = sourceServer.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length)
    Next
End Sub

Paragraphs with Headings (if Heading1, Heading2, etc. style is applied to the headings)

When you apply the Heading style to the paragraph, the paragraph’s outline level changes. Find all paragraphs with the required Paragraph.OutlineLevel property value. Then obtain the range located between these paragraphs (including the paragraph with the heading).

csharp
Document document = server.Document;
List<Paragraph> paragraphs = document.Paragraphs.Where(paragraph => paragraph.OutlineLevel == 1).ToList();
for (int i = 0; i < paragraphs.Count; i++)
  {
    DocumentRange range;
    DocumentPosition startPos = paragraphs[i].Range.Start;
    if (i == paragraphs.Count - 1)
      range = document.CreateRange(startPos, document.Range.End.ToInt() - startPos.ToInt());
    else
      range = document.CreateRange(startPos, paragraphs[i + 1].Range.Start.ToInt() - startPos.ToInt());
  }
vb
For i As Integer = 0 To paragraphs.Count - 1
  Dim tempServer As RichEditDocumentServer = New RichEditDocumentServer()
  Dim tempDoc As Document = tempServer.Document
  Dim range As DocumentRange
  Dim startPos As DocumentPosition = paragraphs(i).Range.Start

  If i = paragraphs.Count - 1 Then
    range = document.CreateRange(startPos, document.Range.[End].ToInt() - startPos.ToInt())
  Else
    range = document.CreateRange(startPos, paragraphs(i + 1).Range.Start.ToInt() - startPos.ToInt())
  End If

  tempDoc.InsertDocumentContent(tempDoc.Range.Start, range)
Next

Text

You can get text from the DocumentRange instance. The table below shows the APIs used to retrieve and insert format-specific content:

FormatRetrieveInsert
Plain TextSubDocument.GetTextSubDocument.AppendText - Appends the specified text.
SubDocument.AppendSingleLineText - Appends text as a single line.
SubDocument.InsertText - Inserts the specified text at the specified position.
SubDocument.InsertSingleLineText - Inserts a single line of text (text without line breaks) at the specified position.
Rich Text FormatSubDocument.GetRtfTextSubDocument.AppendRtfText - Appends formatted text.
SubDocument.InsertRtfText - Inserts formatted text into the specified position.
HTMLSubDocument.GetHtmlTextSubDocument.AppendHtmlText - Appends HTML formatted text.
SubDocument.InsertHtmlText - Inserts the specified HTML text into the specified position.
MHTSubDocument.GetMhtText
WordMLSubDocument.GetWordMLText
DOCX arraySubDocument.GetDocxBytes

Save Document Parts as Separate Files

Call the SubDocument.AppendDocumentContent method to insert the retrieved range to the temporary RichEditDocumentServer instance.

Use the Document.SaveDocument method to save the result. Call the RichEditDocumentServer.ExportToPdf method to export files in PDF format.

The following code snippet saves each document page as a separate RTF file:

csharp
int pageCount = sourceServer.DocumentLayout.GetPageCount();
for (int i = 0; i < pageCount; i++)
  {
    DevExpress.XtraRichEdit.API.Layout.LayoutPage layoutPage = sourceServer.DocumentLayout.GetPage(i);
    DocumentRange mainBodyRange = sourceServer.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length);
    using (RichEditDocumentServer tempServer = new RichEditDocumentServer())
    {
      tempServer.Document.AppendDocumentContent(mainBodyRange);
      //Delete last empty paragraph
      tempServer.Document.Delete(tempServer.Document.Paragraphs.First().Range);
      //Save the result
      tempServer.SaveDocument(String.Format("doc{0}.rtf", i), DocumentFormat.Rtf);  
    }
  }
vb
Dim pageCount As Integer = sourceServer.DocumentLayout.GetPageCount()

For i As Integer = 0 To pageCount - 1
    Dim layoutPage As DevExpress.XtraRichEdit.API.Layout.LayoutPage = sourceServer.DocumentLayout.GetPage(i)
    Dim mainBodyRange As DocumentRange = sourceServer.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length)
    Using tempServer As RichEditDocumentServer = New RichEditDocumentServer()
         tempServer.Document.AppendDocumentContent(mainBodyRange)
         'Delete last empty paragraph
         tempServer.Document.Delete(tempServer.Document.Paragraphs.First().Range)
         'Save the result
         tempServer.SaveDocument(String.Format("doc{0}.rtf", i), DocumentFormat.Rtf)
    End Using
  Next

Insert a Document Part into Another Document

Call the SubDocument.InsertDocumentContent method to insert the obtained DocumentRange in the given position in a document.

Save the result by calling the Document.SaveDocument method. Use the RichEditDocumentServer.ExportToPdf method to export the document to PDF format.

The following code snippet inserts the source document’s second paragraph in another document:

csharp
var server = new RichEditDocumentServer();
server.LoadDocument("Documents//Hans in luck.docx", DocumentFormat.Docx);
DocumentRange retrieveRange = server.Document.Paragraphs[2].Range;

var server1 = new RichEditDocumentServer();
server1.Document.InsertDocumentContent(server1.Document.Range.Start, retrieveRange);
server1.SaveDocument("secondParagraph.docx", DocumentFormat.Docx);
vb
Private Sub SurroundingSub()
Dim server = New RichEditDocumentServer()
server.LoadDocument("Documents//Hans in luck.docx", DocumentFormat.Docx)
Dim retrievedRange As DocumentRange = server.Document.Paragraphs(2).Range

Dim server1 = New RichEditDocumentServer()
server1.Document.InsertDocumentContent(server1.Document.Range.Start, retrieveRange)
server1.SaveDocument("secondParagraph.docx", DocumentFormat.Docx)
End Sub