The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Merge Documents

  • 3 min to read

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

Merge Documents into a DOCX File

  1. Load documents to be merged in separate RichEditDocumentServer instances.
  2. Use one of the following methods to insert the content of one instance into another:

  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 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 manage 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).