Replacement overwrites the existing content within a bookmark range, whereas insertion adds new content at the bookmark position and keeps the existing text. For contracts, reports, and notice templates, insertion is suited to appending supplementary notes, schedules, seals, or appendix body text at fixed placeholders without altering the content already filled in the template.
After locating a bookmark, the BookmarksNavigator of Spire.Doc for C++ can select InsertText(), InsertTable(), InsertParagraph(), or InsertTextBodyPart() according to the content type. The insertion position is determined by the parameters of MoveToBookmark() and can be before or after the start or end marker of the bookmark. All processing takes place in the local process, and the document does not need to be uploaded to any service.
This article covers four features:
The examples use the "Warehouse Scheduling System Implementation Service Agreement" template (ServiceAgreement.docx), the appendix document ScopeAddendum.docx, and the seal image seal.png. The template contains nine bookmarks, and the insertion examples use Budget, ServiceScope, SealAnchor, and AddendumAnchor:
| Bookmark | Inserted content | Main call |
|---|---|---|
Budget |
Amount in words for the contract value | InsertText() |
ServiceScope |
Implementation phase table | InsertTable() |
SealAnchor |
Company seal image | InsertParagraph() |
AddendumAnchor |
Body of the appendix "Supplementary Notes on Service Scope" | InsertTextBodyPart() |
The AddendumBody bookmark in the appendix document covers the entire body, which consists of 15 body objects.

Install Spire.Doc for C++
There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet; the other is to download the package from our website and copy the libraries into your program. Installing through NuGet is simpler and is the recommended approach.
- In Visual Studio, open the NuGet Package Manager, search for
Spire.Doc.Cpp, and install it. - Alternatively, run
nuget install Spire.Doc.Cppin the project directory, and then add the package'sincludeandlibfolders to the project.
An integrated project needs to reference the corresponding import library and to ensure that dependency files such as SpireDocCpp.dll and Spire.Doc.Base.dll are present in the run directory. For detailed configuration instructions, refer to How to Integrate Spire.Doc for C++ in a C++ Application.
Insert Text
BookmarksNavigator->InsertText(text) inserts plain text at the locator, and the original text within the bookmark range remains unchanged. The example inserts the amount in words after the end marker of the Budget bookmark, so the inserted text follows the amount directly.
| Operation | Method |
|---|---|
| Locate a bookmark | BookmarksNavigator->MoveToBookmark(name, isStart, isAfter) |
| Insert plain text | BookmarksNavigator->InsertText(text) |
| Insert a paragraph | BookmarksNavigator->InsertParagraph(paragraph) |
| Insert body content | BookmarksNavigator->InsertTextBodyPart(part) |
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Create a BookmarksNavigator object and call MoveToBookmark("Budget", false, true) to locate the position after the end marker.
- Call InsertText() to insert the amount in words.
- Save the result document with Document->SaveToFile().
The code for inserting text is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/BookmarksNavigator.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Documents;
int main() {
//Load the agreement template that contains bookmarks
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ServiceAgreement.docx");
//Insert the amount in words after the end marker of the Budget bookmark
intrusive_ptr<BookmarksNavigator> navigator = new BookmarksNavigator(document);
navigator->MoveToBookmark("Budget", false, true);
navigator->InsertText(" (in words: four hundred and eighty thousand yuan only)");
//Save the document as docx
document->SaveToFile("Output/InsertText.docx", FileFormat::Docx);
document->Close();
return 0;
}
After insertion, the amount paragraph in Section 4 reads "The total amount of this Agreement is RMB ¥ 480,000 (in words: four hundred and eighty thousand yuan only) (tax inclusive), ...", and both the original amount and the following text are retained:

The Positioning Parameters Determine the Insertion Base
The two Boolean parameters of MoveToBookmark(name, isStart, isAfter) specify whether the start marker or the end marker serves as the reference and whether the insertion point lies after or before that marker. The example uses false, true, which inserts after the end marker, so the amount in words is placed after the Budget bookmark range and before the original phrase "(tax inclusive)". When a bookmark covers an entire passage, choosing a different base changes whether the new content lands before or after the text.
Inserted Text Inherits the Character Formatting at the Locator
Text inserted with InsertText() inherits the character formatting at the locator. In the example, the Budget bookmark covers the bold red amount, so the inserted amount in words is also displayed in bold red. When the inserted text needs independent formatting, use InsertParagraph() to build a paragraph with its own character formatting, or adjust the formatting of the text outside the bookmark range.
Insertion Versus Replacement
InsertText() adds text at the bookmark position, and both the content within the bookmark range and the bookmark markers are retained. ReplaceBookmarkContent() replaces the content within the bookmark range. Use insertion when supplementary notes need to be appended at a placeholder, and use replacement when the placeholder text needs to be replaced entirely with a new value.
Insert a Table
BookmarksNavigator->InsertTable(table) inserts a table at the locator. The example inserts a four-row, three-column implementation phase table at the ServiceScope bookmark; the table is placed at the start of Section 2 as an overview of the service scope, and the three paragraphs covered by the bookmark remain as they are.
| Operation | Method |
|---|---|
| Create a table | new Table(document, true) |
| Build the row and column structure | Table->ResetCells(rows, columns) |
| Obtain a cell | Table->GetRows()->GetItem(r)->GetCells()->GetItem(c) |
| Insert the table | BookmarksNavigator->InsertTable(table) |
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Create a Table object and call ResetCells() to build a four-row, three-column structure.
- Call AddParagraph() for the header cells to add paragraphs, and set bold text, white text, and dark blue shading.
- Fill the data rows with the implementation phase, planned duration, and key deliverables.
- Create a BookmarksNavigator object, locate
ServiceScope, and call InsertTable() to insert the table. - Save the result document with Document->SaveToFile().
The code for inserting a table is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/BookmarksNavigator.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Documents/HorizontalAlignment.h"
#include "Spire/Doc/Formatting/ParagraphFormat.h"
#include "Spire/Doc/Table.h"
#include "Spire/Doc/TableRow.h"
#include "Spire/Doc/TableCell.h"
#include "Spire/Doc/Collections/RowCollection.h"
#include "Spire/Doc/Collections/CellCollection.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/Formatting/CharacterFormat.h"
#include "Spire/Doc/Formatting/CellFormat.h"
#include "Spire/Doc/Fields/Shapes/Shading.h"
#include "Spire/Doc/common/h/Color.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Common;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;
using namespace Spire::Doc::Formatting;
int main() {
//Load the agreement template that contains bookmarks
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ServiceAgreement.docx");
//Build the implementation phase table
intrusive_ptr<Table> table = new Table(document, true);
table->ResetCells(4, 3);
const char* headers[3] = {"Implementation Phase", "Planned Duration", "Key Deliverables"};
for (int c = 0; c < 3; c++)
{
intrusive_ptr<TableCell> cell = table->GetRows()->GetItem(0)->GetCells()->GetItem(c);
intrusive_ptr<Paragraph> paragraph = cell->AddParagraph();
paragraph->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
intrusive_ptr<TextRange> range = paragraph->AppendText(headers[c]);
range->GetCharacterFormat()->SetBold(true);
range->GetCharacterFormat()->SetTextColor(Color::GetWhite());
cell->GetCellFormat()->GetShading()->SetBackgroundPatternColor(
Color::FromArgbRGB(31, 56, 100));
}
const char* rows[3][3] = {
{"Project Kickoff", "4 weeks", "Requirements Specification"},
{"System Deployment", "8 weeks", "Deployment and interface integration report"},
{"Trial Run and Acceptance", "6 weeks", "Trial run records and acceptance confirmation"}};
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
intrusive_ptr<TableCell> cell = table->GetRows()->GetItem(r + 1)->GetCells()->GetItem(c);
intrusive_ptr<Paragraph> paragraph = cell->AddParagraph();
paragraph->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Left);
paragraph->AppendText(rows[r][c]);
}
}
//Insert the table at the ServiceScope bookmark, which places it at the start of Section 2
intrusive_ptr<BookmarksNavigator> navigator = new BookmarksNavigator(document);
navigator->MoveToBookmark("ServiceScope");
navigator->InsertTable(table);
//Save the document as docx
document->SaveToFile("Output/InsertTable.docx", FileFormat::Docx);
document->Close();
return 0;
}
After insertion, Section 2 contains, in order, the heading, the implementation phase table, the service scope description, and the appendix line:

The Table Position Is Relative to the Bookmark
MoveToBookmark("ServiceScope") moves the locator to the start of the bookmark range, and InsertTable() inserts the table from that position. For a bookmark that covers several paragraphs, the table lands at the start of the range, and the paragraphs within the bookmark shift down in order. When the table needs to follow the bookmark range, use the empty paragraph after the bookmark or the next bookmark as the locator, and then adjust the isStart and isAfter parameters.
Cells Have No Default Paragraph After ResetCells
Table->ResetCells(rows, columns) creates only the row and column structure, and the cells contain no ready-made paragraph. Before filling content, call TableCell->AddParagraph() to add a paragraph and then call Paragraph->AppendText() to write the text. The bold text, white text, and dark blue shading of the header need to be set cell by cell, and the inserted table does not automatically inherit the style of other tables in the template.
Inserting a Table Versus Replacing Bookmark Content
InsertTable() inserts a table at the bookmark position, and the bookmark range and the original body text remain unchanged. ReplaceBookmarkContent(TextBodyPart) replaces the content within the bookmark range with a table. Use insertion when a schedule needs to be appended after a placeholder paragraph, and use replacement when the placeholder paragraph needs to be replaced entirely with a table.
Insert an Image
InsertParagraph() accepts a paragraph object, not a picture object. The example first creates a paragraph in a temporary section, calls Paragraph->AppendPicture() to load the seal image and set its width and height, then inserts the whole paragraph at the SealAnchor bookmark, and finally removes the temporary section.
| Operation | Method |
|---|---|
| Locate a bookmark | BookmarksNavigator->MoveToBookmark(name, isStart, isAfter) |
| Create a paragraph that contains the picture | Paragraph->AppendPicture(fileName) |
| Set the picture size | DocPicture->SetWidth() / SetHeight() |
| Insert the paragraph | BookmarksNavigator->InsertParagraph(paragraph) |
| Remove the temporary section | SectionCollection->Remove(section) |
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Create a BookmarksNavigator object and call MoveToBookmark("SealAnchor", true, true) to locate the position after the start marker.
- Call Document->AddSection() to create a temporary section, add a paragraph to it, and call AppendPicture() to load the seal image.
- Call DocPicture->SetWidth() and SetHeight() to set the picture size to 72 points.
- Call InsertParagraph() to insert the picture paragraph at the bookmark position.
- Call SectionCollection->Remove() to remove the temporary section.
- Save the result document with Document->SaveToFile().
The code for inserting an image is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#ifdef LoadImage
#undef LoadImage
#endif
#define LoadImage LoadImageA
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/Collections/SectionCollection.h"
#include "Spire/Doc/Documents/BookmarksNavigator.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/DocPicture.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;
int main() {
//Load the agreement template that contains bookmarks
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ServiceAgreement.docx");
//Insert the seal image after the start marker of the SealAnchor bookmark
intrusive_ptr<BookmarksNavigator> navigator = new BookmarksNavigator(document);
navigator->MoveToBookmark("SealAnchor", true, true);
//Build a paragraph that contains the picture in a temporary section
intrusive_ptr<Section> temp = document->AddSection();
intrusive_ptr<Paragraph> paragraph = temp->AddParagraph();
intrusive_ptr<DocPicture> picture = paragraph->AppendPicture("seal.png");
picture->SetWidth(72.0f);
picture->SetHeight(72.0f);
navigator->InsertParagraph(paragraph);
//Remove the temporary section used only to build the picture paragraph
document->GetSections()->Remove(temp);
//Save the document as docx
document->SaveToFile("Output/InsertPicture.docx", FileFormat::Docx);
document->Close();
return 0;
}
The seal image is inserted at the SealAnchor bookmark of the signature table, below the Party A name:

The Picture Must Be Placed in a Paragraph First
InsertParagraph() accepts a paragraph object, not a picture object. The example first creates a paragraph in a temporary section, calls AppendPicture() to load the seal image, sets the width and height to 72 points, and then inserts the whole paragraph at the bookmark position. Calling an insertion method on a picture directly has no corresponding overload.
The Temporary Section Should Be Removed After Insertion
The temporary section in the example is used only to build the paragraph that contains the picture and does not take part in the final layout. After the paragraph is inserted into the target document, call SectionCollection->Remove() to remove the temporary section; otherwise, a blank page is added at the end of the result document. The inserted paragraph belongs to the target document, and removing the temporary section does not affect content that has already been inserted.
Image Size and Position Need to Be Adjusted for the Cell
The example sets the picture width and height to 72 points, which suits the size of the signature table cell. When the picture is too large, the cell grows taller automatically and may push the table onto the next page. When the bookmark is located in a table cell, confirm the cell width and row height before setting the picture size. The example uses true, true for positioning, so the picture is placed after the start marker of SealAnchor; changing the parameters to false, true places the picture after the end marker of the bookmark.
Insert Content from Another Document
BookmarksNavigator->InsertTextBodyPart(part) inserts the paragraphs and tables carried by a TextBodyPart at the bookmark position. The example obtains the entire body from the AddendumBody bookmark of the appendix document ScopeAddendum.docx and inserts it at the AddendumAnchor bookmark of the agreement template.
| Operation | Method |
|---|---|
| Locate a bookmark in the source document | BookmarksNavigator->MoveToBookmark(name) |
| Obtain the source bookmark content | BookmarksNavigator->GetBookmarkContent() |
| Locate a bookmark in the target document | BookmarksNavigator->MoveToBookmark(name) |
| Insert body content | BookmarksNavigator->InsertTextBodyPart(part) |
The detailed steps are as follows.
- Create two Document objects and load the agreement template and the appendix document respectively.
- Create a BookmarksNavigator object in the appendix document, and call MoveToBookmark("AddendumBody") and GetBookmarkContent() to obtain the appendix body.
- Create a BookmarksNavigator object in the agreement template, and call MoveToBookmark("AddendumAnchor") to locate the appendix line.
- Call InsertTextBodyPart() to insert the appendix body at the bookmark position.
- Save the result document with Document->SaveToFile().
The code for inserting content from another document is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/BookmarksNavigator.h"
#include "Spire/Doc/Documents/TextBodyPart.h"
#include "Spire/Doc/Collections/BodyRegionCollection.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Documents;
int main() {
//Load the agreement template and the addendum document
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ServiceAgreement.docx");
intrusive_ptr<Document> addendum = new Document();
addendum->LoadFromFile("ScopeAddendum.docx");
//Obtain the body content of the addendum
intrusive_ptr<BookmarksNavigator> addendumNav = new BookmarksNavigator(addendum);
addendumNav->MoveToBookmark("AddendumBody");
intrusive_ptr<TextBodyPart> part = addendumNav->GetBookmarkContent();
cout << "Addendum body item count: " << part->GetBodyItems()->GetCount() << endl;
//Insert the addendum content at the AddendumAnchor bookmark
intrusive_ptr<BookmarksNavigator> navigator = new BookmarksNavigator(document);
navigator->MoveToBookmark("AddendumAnchor");
navigator->InsertTextBodyPart(part);
//Save the document as docx
document->SaveToFile("Output/InsertDocument.docx", FileFormat::Docx);
document->Close();
addendum->Close();
return 0;
}
The bookmark in the appendix document covers the title, information strip, note box, two body paragraphs, and a four-row, three-column table, for a total of 15 body objects. The console output is as follows:
Addendum body item count: 15
Content inserted at the AddendumAnchor bookmark retains the title area, information strip, and note box of the appendix document, and the original appendix line remains before the inserted content:

The Source Document Needs a Bookmark That Covers the Whole Block
GetBookmarkContent() returns only the body objects within the bookmark range. The example creates the AddendumBody bookmark in the appendix document in advance to cover the entire body, so the title, information strip, note box, body paragraphs, and table are obtained in one call. When the source document has no ready-made bookmark, call AppendBookmarkStart() and AppendBookmarkEnd() to enclose the target content first, or extract paragraphs and tables one by one and insert them separately.
Cross-Document Insertion Retains Source Formatting
InsertTextBodyPart() writes the objects in the TextBodyPart into the target document, and paragraph formatting and character formatting are inserted along with the content. In the example, the appendix title, the shading of the information strip, and the table column widths match the source document. When the page width of the target document differs from that of the source document, the table may need its column widths adjusted, and the indentation of the title and the note box may need to be set again.
Relationship Between Inserted Content and the Bookmark Position
InsertTextBodyPart() inserts content at the bookmark locator, and the bookmark range and the original text remain unchanged. The example inserts the appendix body at the start of the AddendumAnchor bookmark, so the appendix content follows the sentence "Appendix: Supplementary Notes on Service Scope (attached separately)." When the appendix content needs to start on a new page after the bookmark, set a page break in the inserted content, or adjust the page break property of the paragraphs after insertion.
Get a Free License
If you want to remove the evaluation message from the generated documents, or to get rid of the function limitations, please apply for a 30-day temporary license.
