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

Merge Documents

  • 3 minutes to read

This topic describes how to use the RichEditDocumentServer to merge documents and keep document formatting.

Merge Documents

  1. Load documents to be merged in separate RichEditDocumentServer instances.
  2. Insert the content of one server into another using one of the following methods:

  3. Call the IRichEditDocumentServer.SaveDocument method to save the resulting document.

    
    var richEditDocumentServer1 = new RichEditDocumentServer();
    richEditDocumentServer1.LoadDocument("document1.docx", DocumentFormat.OpenXml);
    var richEditDocumentServer2 = new RichEditDocumentServer();
    richEditDocumentServer2.LoadDocument("document2.docx", DocumentFormat.OpenXml);
    
    richEditDocumentServer1.Document.AppendDocumentContent(richEditDocumentServer2.Document.Range);
    
    richEditDocumentServer1.SaveDocument("result.docx", DocumentFormat.OpenXml);
    

Merge Documents into a PDF File

Call the RichEditDocumentServer.ExportToPdf method to export the result to a PDF file. You can also export all documents to separate PDF files and merge them using the PDF Document API. Refer to the How to: Merge Documents into a Single PDF File topic for more information.

Keep Document Formatting

Direct Formatting and Styles

Pass one of InsertOptions enumeration members to SubDocument.AppendDocumentContent or SubDocument.InsertDocumentContent method as an insertOptions parameter to manage how the formatting is applied to the inserted content. The table below shows how formatting is applied depending on the selected value:

Enumeration Value

Direct Formatting

Styles (character, paragraph, table)

Non-textual objects (tables, floating objects, comments, etc.)

Tabs

InsertOptions.KeepSourceFormatting

The inserted text retains its formatting.

New styles (styles that don’t exist in the target document) are copied. If character or paragraph style conflict occurs, all inserted style’s properties are applied as direct formatting, but the style itself is not transferred. Destination document’s styles with default settings are substituted with inserted styles of the same name. Conflicted table and numbering list styles are copied to the document. The style name is a combination of conflicted styles’ names (e.g., Normal1 and Normal1 are combined into_Normal11_).

Non-textual elements are retained.

All page properties are retained.

InsertOptions.KeepTextOnly

The inserted text loses its formatting and takes on direct formatting applied to the target text range. Numbering lists are converted to simple text.

All inserted styles are not retained.

Floating objects, shapes and comments are discarded. Tables are converted into a series of paragraphs.

Tabs are lost.

InsertOptions.MergeFormatting

The inserted text retains the following character properties:

Other character properties and all paragraph properties are ignored.

Inserted style’s character properties are applied as direct, but the style itself is not copied to the target document. Style paragraph properties are ignored. Table styles are not retained. Numbering list styles are transferred to the target document.

Non-textual elements are retained.

Tabs are ignored.

InsertOptions.UseDestinationStyles

The inserted text keeps its format options.

New styles (styles that don’t exist in the target document) are copied. If style conflict occurs, the inserted content’s styles are ignored. Destination document’s styles with default settings are substituted with inserted styles of the same name.

Non-textual elements are retained.

All page properties are retained.

Note

The RichEditDocumentServer does not currently support themes. When merging documents with themes, the result may lose theme-related formatting.