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

Merge Documents

  • 4 minutes to read

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

Merge Documents into a DOCX File

Use one of the following methods to insert the content of one document into another:

Call the RichEditDocumentServer.SaveDocument method to save the resulting document.

var wordProcessor = new RichEditDocumentServer();
wordProcessor.LoadDocument("document1.docx");

wordProcessor.Document.AppendDocumentContent("document2.docx");

wordProcessor.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 use PDF Document API to export all documents to separate PDF files and merge them. Refer to the Merge Documents 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 specify how the formatting is applied to the inserted content.

The table below shows how formatting is applied to the pasted document elements depending on the InsertOptions property value:

Enumeration Value

Direct Formatting

Styles (character, paragraph, table)

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

Page Properties and 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 conflicts occur, all inserted style’s properties are applied as direct formatting, but the style itself is not transferred. Destination document styles with default settings are substituted with inserted styles. 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.

All page properties, including tabs, are lost.

InsertOptions.MatchDestinationFormatting

The inserted text retains its formatting.

All inserted styles are not retained.

Non-textual elements are retained.

All page properties are retained.

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.

Page properties 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 conflicts occur, the inserted content’s styles are ignored. Destination document’s styles with default settings are substituted with inserted styles.

Non-textual elements are retained.

All page properties are retained.

Note

When you merge documents with themes, the result may lose theme-related formatting (color, paragraph spacing).

Sections

When the inserted content is divided into sections, the inserted and destination sections interact as follows:

  • If the content is inserted into an empty document, all inserted section properties are retained.
  • If the content is inserted into a document divided into sections, all but the last inserted sections retain their parameters. The last section takes on the destination section’s options.
  • If the content is inserted after a section break, both destination and inserted sections retain their properties. Use the Document.InsertSection method to insert a section break at the specified document position.

  • If the content is copied without section properties (the last section’s paragraph is not included), the destination section’s settings stay intact after insertion.