Cookbook

Merge anywhere on the document

The MultiMerge class, available for Advanced and Premium licenses, allows to merge two or more DOCX. This merge works in a sequential way: one document after the other, one at a time, from the first to the last one.

Thanks to the joint use of DOCXPath and MultiMerge, you can alter this behavior and merge documents from any part of the document. For example, you could merge the second document after a specific section of the first one.

To do this kind of merging in any place of the document you have to rely on a placeholder that you must delete before you create the DOCX.

Steps to follow

  • 1. Insert a new placeholder at the end of the first DOCX with the method insertWordFragment of DOCXPath.
  • 2. Merge the following document with mergeDocx. The merge is done automatically after the placeholder of the previous step.
  • 3. Move the contents of the second document to the desired position with the moveWordContent method of DOCXPath.
  • 4. Remove the placeholder of step 1 with either the method removeWordContent or removeTemplateVariable.

Practical example

As an example, let's merge two documents and place the contents of the second one after the first paragraph of the first one.

This script can move all contents of the second document, but thanks to DOCXPath it is possible to move just a few of them, delete them or insert new ones.

In-memory DOCX merging (Premium licenses only)

Premium licenses support In-memory DOCX merging, that is, merging that doesn't require creating files.

Related information