---
title: "Insert and Append Documents in C++"
---


Sometimes it is required to combine several documents into one. You can do this manually or you can use Aspose.Words insert or append feature.

The insert operation allows you to insert the content of previously created documents into a new or existing one.

In turn, the append feature allows you to add a document only at the end of another document.

This article explains how to insert or append a document to another one in different ways and describes the common properties that you can apply while inserting or appending documents.

## Insert a Document

As mentioned above, in Aspose.Words a document is represented as a tree of nodes, and the operation of inserting one document into another is copying nodes from the first document tree to the second one.

You can insert documents in a variety of locations in different ways. For example, you can insert a document through a replace operation, a merge field during a merge operation, or via a bookmark.

You can also use the [InsertDocument](https://reference.aspose.com/words/cpp/aspose.words/documentbuilder/insertdocument/) method, which is similar to inserting a document in Microsoft Word, to insert a whole document at the current cursor position without any previous importing.

The following code example shows how to insert a document using the `InsertDocument` method:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-with-builder.h" >}}

The following subsections describe the options during which you can insert one document into another.

### Insert a Document During Find and Replace Operation {#insert-a-document-during-find-and-replace-operation}

You can insert documents while performing find and replace operations. For example, a document can contain paragraphs with the text [INTRODUCTION] and [CONCLUSION]. But in the final document, you need to replace those paragraphs with the content obtained from another external document. To achieve that, you will need to create a handler for the replace event.

The following code example shows how to create a handler for the replacing event to use it later in the inserting process:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-at-replace-handler.h" >}}

The following code example shows how insert content of one document into another during a find and replace operation:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-at-replace.h" >}}

### Insert a Document During Mail Merge Operation {#insert-a-document-during-mail-merge-operation}

You can insert a document into a merge field during a Mail Merge operation. For example, a Mail Merge template can contain a merge field such as [Summary]. But in the final document, you need to insert content obtained from another external document into this merge field. To achieve that, you will need to create a handler for the merge event.

The following code example shows how to create a handler for the merging event to use it later in the inserting process:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-at-mail-merge-handler.h" >}}

The following code example shows how to insert a document into the merge field using the created handler:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-at-mail-merge.h" >}}

### Insert a Document at Bookmark

You can import a text file into a document and insert it right after a bookmark that you have defined in the document. To do this, create a bookmarked paragraph where you want the document to be inserted.

The following coding example shows how to insert the contents of one document to a bookmark in another document:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-at-bookmark.h" >}}

{{% alert color="primary" %}}

Note that the bookmark should not enclose multiple paragraphs or text that you want them to appear in your final resulting document.

{{% /alert %}}

## Append a Document

You may have a use case where you need to include additional pages from a document to the end of an existing document. To do this, you just need to call the [AppendDocument](https://reference.aspose.com/words/cpp/aspose.words/document/appenddocument/) method to add a document to the end of another one.

{{% alert color="primary" %}}

Note that [AppendChild](https://reference.aspose.com/words/cpp/aspose.words/compositenode/appendchild/) is a node level method within a document. For example, you can create a paragraph, set formatting properties, and then append it as a child to the body using the **AppendChild** method.

{{% /alert %}}

The following code example shows how to append a document to the end of another document:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "keep-source-formatting.h" >}}

## Import and Insert Nodes Manually {#import-and-insert-nodes-manually}

Aspose.Words allows you to insert and append documents automatically without any previous importing requirements. However, if you need to insert or append a specific node of your document, such as a section or a paragraph, then first you need to import this node manually.

When you need to insert or append one section or paragraph to another, you essentially need to import the nodes of the first document node tree into the second one using the [ImportNode](https://reference.aspose.com/words/cpp/aspose.words/nodeimporter/importnode/) method. After importing your nodes, you need to use the [InsertAfter](https://reference.aspose.com/words/cpp/aspose.words/compositenode/insertafter/)/[InsertBefore](https://reference.aspose.com/words/cpp/aspose.words/compositenode/insertbefore/) method to insert a new node after/before the reference node. This allows you to customize the inserting process by importing nodes from a document and inserting it at given positions.

You can also use the [AppendChild](https://reference.aspose.com/words/cpp/aspose.words/compositenode/appendchild/) method to add a new specified node to the end of the list of child nodes, for example, if you want to append content at the paragraph level instead of at the section level.

The following code example shows how to manually import nodes and insert them after a specific node using the **InsertAfter** method:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "insert-document-as-nodes.h" >}}

{{% alert color="primary" %}}

The import creates a new node that is a copy of the original node and suitable for insertion into the destination document.

{{% /alert %}}

Content is imported into the destination document section by section, which means that settings, such as page setup and headers or footers, are preserved during import. It is also useful to note that you can define formatting settings when you insert or append a document to specify how two documents are joined together.

## Common Properties for Insert and Append Documents {#common-properties-for-insert-and-append-documents}

Both [InsertDocument](https://reference.aspose.com/words/cpp/aspose.words/documentbuilder/insertdocument/) and [AppendDocument](https://apireference.codeporting.com/native/cs2cpp/namespace/system#a6b77ccd8c49df28c153be0462cdfdf49) methods accept [ImportFormatMode](https://reference.aspose.com/words/cpp/aspose.words/importformatmode/) and [ImportFormatOptions](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/) as input parameters. The **ImportFormatMode** allows you to control how document formatting is merged when you import content from one document into another by selecting different format modes such as [UseDestinationStyles](https://reference.aspose.com/words/cpp/aspose.words/importformatmode/), [KeepSourceFormatting](https://reference.aspose.com/words/cpp/aspose.words/importformatmode/), and [KeepDifferentStyles](https://reference.aspose.com/words/cpp/aspose.words/importformatmode/). The **ImportFormatOptions** allows you to select different import options such as [IgnoreHeaderFooter](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/get_ignoreheaderfooter/), [IgnoreTextBoxes](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/get_ignoretextboxes/), [KeepSourceNumbering](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/get_keepsourcenumbering/), [MergePastedLists](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/get_mergepastedlists/), and [SmartStyleBehavior](https://reference.aspose.com/words/cpp/aspose.words/importformatoptions/get_smartstylebehavior/).

Aspose.Words allows you to adjust the visualization of a resulting document when two documents are added together in an insert or append operation by using the [Section](https://reference.aspose.com/words/cpp/aspose.words/section/) and [PageSetup](https://reference.aspose.com/words/cpp/aspose.words/pagesetup) properties. The **PageSetup** property contains all the attributes of a section such as [SectionStart](https://reference.aspose.com/words/cpp/aspose.words/pagesetup/get_sectionstart/), [RestartPageNumbering](https://reference.aspose.com/words/cpp/aspose.words/pagesetup/get_restartpagenumbering/), [PageStartingNumber](https://reference.aspose.com/words/cpp/aspose.words/pagesetup/get_pagestartingnumber/), [Orientation](https://reference.aspose.com/words/cpp/aspose.words/pagesetup/get_orientation/), and others. The most common use case is to set the **SectionStart** property to define if the added content will appear on the same page or split into a new one.

{{% alert color="primary" %}}

Note that the **Section** and **PageSetup** properties do not control how two documents are inserted/appended together. They only allow you to change the appearance of your result document.

{{% /alert %}}

The following code example shows how to append one document to another while keeping the content from splitting across two pages:

{{< gist "aspose-words-gists" "34934bfeabca5cf3dd3ca3c277e85771" "different-page-setup.h" >}}

------ 

## FAQ

1. **Q:** How do I apply a license for Aspose.Words in a C++ project?  
   **A:** Create an `Aspose::Words::License` object, load the license file (e.g., `Aspose.Words.lic`) using `SetLicense`, and keep the object alive for the lifetime of the application. Example:  

   ```cpp
   System::SharedPtr<Aspose::Words::License> license = System::MakeObject<Aspose::Words::License>();
   license->SetLicense(u"../Licenses/Aspose.Words.lic");
   ```

2. **Q:** What is the difference between `InsertDocument` and `AppendDocument`?  
   **A:** `InsertDocument` inserts the source document at the current cursor position of a `DocumentBuilder`, preserving the surrounding content. `AppendDocument` adds the source document to the end of the target document without needing a builder. Use `InsertDocument` when you need to place content in the middle; use `AppendDocument` for simple concatenation.

3. **Q:** How can I keep the original formatting of the source document when inserting or appending?  
   **A:** Pass `ImportFormatMode::KeepSourceFormatting` (or `KeepDifferentStyles`) to the method. For example:  

   ```cpp
   builder->InsertDocument(sourceDoc, Aspose::Words::ImportFormatMode::KeepSourceFormatting);
   targetDoc->AppendDocument(sourceDoc, Aspose::Words::ImportFormatMode::KeepSourceFormatting);
   ```

4. **Q:** How do I insert a document at a specific bookmark?  
   **A:** Move the `DocumentBuilder` to the bookmark with `MoveToBookmark("MyBookmark")` and then call `InsertDocument`. Example:  

   ```cpp
   System::SharedPtr<Aspose::Words::DocumentBuilder> builder = System::MakeObject<Aspose::Words::DocumentBuilder>(targetDoc);
   builder->MoveToBookmark(u"MyBookmark");
   builder->InsertDocument(sourceDoc, Aspose::Words::ImportFormatMode::UseDestinationStyles);
   ```

5. **Q:** How can I import only a single node (e.g., a paragraph) from one document to another?  
   **A:** Use `NodeImporter::ImportNode` to clone the node, then insert it with `InsertAfter` or `AppendChild`. Example:  

   ```cpp
   System::SharedPtr<Aspose::Words::Node> para = sourceDoc->get_FirstSection()->get_Body()->get_Paragraphs()->idx_get(0);
   System::SharedPtr<Aspose::Words::NodeImporter> importer = System::MakeObject<Aspose::Words::NodeImporter>(para, targetDoc, Aspose::Words::ImportFormatMode::UseDestinationStyles);
   System::SharedPtr<Aspose::Words::Node> importedPara = importer->ImportNode(para, true);
   targetDoc->get_FirstSection()->get_Body()->AppendChild(importedPara);
   ```