Captions provide fixed numbering and explanatory text for pictures, tables, and equations, and the body reuses that number through cross-references. Caption numbers are generated by SEQ fields; chapter-numbered captions also reference the number of the Heading 1 chapter; a cross-reference locates the caption with a bookmark and then displays the caption text and page number through REF and PAGEREF fields. Together these three form the caption system in Word, and both the numbering and the reference results depend on field update.
Spire.Doc for C++ adds captions through DocPicture->AddCaption() and Table->AddCaption(), builds STYLEREF and SEQ fields through Field and SequenceField, and creates cross-references through BookmarkStart, BookmarkEnd, and REF and PAGEREF fields. All processing is completed in the local process, and the document does not need to be uploaded to any service.
This article covers three features:
The example uses the East China Retail Terminal Market Analysis Report for H1 2026 (MarketReport.docx). The document has two pages. Page 1 contains a sales bar chart and a regional sales table, and page 2 covers channel trends and strategy recommendations. The four Heading 1 chapters use the Heading 1 style and are numbered 1 to 4, providing the chapter numbers referenced by chapter-numbered captions.

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.
Add Captions to Pictures and Tables
AddCaption() inserts a caption paragraph next to a picture or table, and the paragraph contains a SEQ field. Pictures call it through a DocPicture object, and tables through a Table object. The label, numbering format, and position are specified by three parameters.
| Operation | Method |
|---|---|
| Add a caption to a picture | DocPicture->AddCaption(label, format, position) |
| Add a caption to a table | Table->AddCaption(label, format, position) |
| Numbering format | CaptionNumberingFormat::Number, Roman, Alphabetic |
| Caption position | CaptionPosition::AboveItem, BelowItem |
| Update fields on save | Document->SetIsUpdateFields(true) |
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Traverse the body objects and locate the chart object by the DocPicture type.
- Call AddCaption() to add a caption below the picture, with the label "Figure".
- Find the table after the "Regional Sales Structure" heading and call AddCaption() to add a caption above it, with the label "Table".
- Call SetIsUpdateFields() to update fields on save, and then save the document with SaveToFile().
The code for adding captions 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/Section.h"
#include "Spire/Doc/Body.h"
#include "Spire/Doc/DocumentObject.h"
#include "Spire/Doc/Collections/SectionCollection.h"
#include "Spire/Doc/Collections/DocumentObjectCollection.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/DocPicture.h"
#include "Spire/Doc/Table.h"
#include "Spire/Doc/CaptionNumberingFormat.h"
#include "Spire/Doc/CaptionPosition.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 market report without captions
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("MarketReport.docx");
intrusive_ptr<Section> section = document->GetSections()->GetItem(0);
//Find the picture object in the body
intrusive_ptr<DocPicture> picture = nullptr;
intrusive_ptr<DocumentObjectCollection> bodyItems = section->GetBody()->GetChildObjects();
for (int i = 0; i < bodyItems->GetCount() && picture == nullptr; i++)
{
intrusive_ptr<DocumentObject> item = bodyItems->GetItem(i);
if (!Object::CheckType<Paragraph>(item))
continue;
intrusive_ptr<Paragraph> paragraph = Object::Dynamic_cast<Paragraph>(item);
for (int j = 0; j < paragraph->GetChildObjects()->GetCount(); j++)
{
intrusive_ptr<DocumentObject> child = paragraph->GetChildObjects()->GetItem(j);
if (Object::CheckType<DocPicture>(child))
{
picture = Object::Dynamic_cast<DocPicture>(child);
break;
}
}
}
//Add a figure caption below the picture
if (picture != nullptr)
picture->AddCaption("Figure", CaptionNumberingFormat::Number, CaptionPosition::BelowItem);
//Add a table caption above the table in the "Regional Sales Structure" section
intrusive_ptr<Table> table = nullptr;
bool inSalesSection = false;
for (int i = 0; i < bodyItems->GetCount() && table == nullptr; i++)
{
intrusive_ptr<DocumentObject> item = bodyItems->GetItem(i);
if (Object::CheckType<Paragraph>(item))
{
intrusive_ptr<Paragraph> paragraph = Object::Dynamic_cast<Paragraph>(item);
if (paragraph->GetText().find("Regional Sales Structure") != string::npos)
inSalesSection = true;
}
else if (inSalesSection && Object::CheckType<Table>(item))
{
table = Object::Dynamic_cast<Table>(item);
}
}
if (table != nullptr)
table->AddCaption("Table", CaptionNumberingFormat::Number, CaptionPosition::AboveItem);
//Update the SEQ fields in the captions when the document is saved
document->SetIsUpdateFields(true);
document->SaveToFile("Output/AddCaptions.docx", FileFormat::Docx);
document->Close();
return 0;
}
After saving, "Figure 1" appears below the picture and "Table 1" appears above the table. Caption numbers are generated by fields; after pictures or tables are inserted or deleted, updating the fields again renumbers them.

A Caption Is a Paragraph with a Field
AddCaption() generates a caption paragraph that contains a SEQ field, and the number shown on the page comes from the field result. The example calls SetIsUpdateFields(true) before saving, so the sequence number is calculated when the document is saved. After pictures are inserted or deleted, the fields should be updated again; otherwise the numbers may not match the actual order.
The Position Parameter Determines Caption Placement
CaptionPosition::BelowItem places the caption below the object, and AboveItem places it above. Picture captions are usually placed below and table captions above. The enumeration also provides aliases such as AfterImage and AboveImage, with the same meanings as BelowItem and AboveItem.
Locate the Correct Table Object
A loaded document may contain several tables, and information bars and callout boxes are also table objects. The example does not read the first element of Section->GetTables() directly; it first locates the "Regional Sales Structure" heading and then takes the first table after that heading. This avoids adding captions to an information bar or callout box.
Add Chapter-Numbered Captions
A chapter-numbered caption is formed by combining a STYLEREF field with a SEQ field. STYLEREF 1 \s reads the number of the Heading 1 chapter, and SEQ generates the caption sequence number; the two are joined by a hyphen to form "Figure 2-1". The caption paragraph must be inserted after the picture paragraph.
| Operation | Method |
|---|---|
| Reference the heading number | Field + FieldType::FieldStyleRef, field code STYLEREF 1 \s |
| Generate the caption number | SequenceField + FieldType::FieldSequence |
| Set the caption name | SequenceField->SetCaptionName() |
| Set the numbering format | SequenceField->SetNumberFormat() |
| Insert the caption paragraph | Body->GetChildObjects()->Insert(index, paragraph) |
The detailed steps are as follows.
- Create a Document object and load the sample document.
- Traverse the body objects and record the index of the paragraph that holds the picture.
- Create a Paragraph object as the caption paragraph, and set it to center alignment with spacing after.
- Append the text "Figure ", then append a FieldStyleRef field and set its field code.
- Append a hyphen, then append a SequenceField, and set the caption name and numbering format.
- Call Insert() to insert the caption paragraph after the picture paragraph, enable field update, and save.
The code for adding chapter-numbered captions 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/Section.h"
#include "Spire/Doc/Body.h"
#include "Spire/Doc/DocumentObject.h"
#include "Spire/Doc/Collections/SectionCollection.h"
#include "Spire/Doc/Collections/DocumentObjectCollection.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Documents/HorizontalAlignment.h"
#include "Spire/Doc/Fields/DocPicture.h"
#include "Spire/Doc/Fields/Field.h"
#include "Spire/Doc/Fields/SequenceField.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/Formatting/CharacterFormat.h"
#include "Spire/Doc/Formatting/ParagraphFormat.h"
#include "Spire/Doc/CaptionNumberingFormat.h"
#include "Spire/Doc/FieldType.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::Documents;
using namespace Spire::Doc::Fields;
using namespace Spire::Doc::Formatting;
using namespace Spire::Doc::Common;
int main() {
//Load the market report without captions
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("MarketReport.docx");
intrusive_ptr<Section> section = document->GetSections()->GetItem(0);
intrusive_ptr<DocumentObjectCollection> bodyItems = section->GetBody()->GetChildObjects();
//Find the body object that holds the picture and record its index
int pictureIndex = -1;
for (int i = 0; i < bodyItems->GetCount() && pictureIndex < 0; i++)
{
intrusive_ptr<DocumentObject> item = bodyItems->GetItem(i);
if (!Object::CheckType<Paragraph>(item))
continue;
intrusive_ptr<Paragraph> paragraph = Object::Dynamic_cast<Paragraph>(item);
for (int j = 0; j < paragraph->GetChildObjects()->GetCount(); j++)
{
if (Object::CheckType<DocPicture>(paragraph->GetChildObjects()->GetItem(j)))
{
pictureIndex = i;
break;
}
}
}
if (pictureIndex < 0)
{
cout << "Picture not found" << endl;
document->Close();
return 1;
}
//Create the caption paragraph: label + chapter number + sequence number
intrusive_ptr<Paragraph> caption = new Paragraph(document);
caption->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
caption->GetFormat()->SetAfterSpacing(6);
intrusive_ptr<TextRange> label = caption->AppendText("Figure ");
label->GetCharacterFormat()->SetFontSize(9);
label->GetCharacterFormat()->SetTextColor(Color::FromArgbRGB(90, 99, 112));
//STYLEREF 1 \s reads the number of the Heading 1 chapter
intrusive_ptr<Field> chapter = caption->AppendField("Heading", FieldType::FieldStyleRef);
chapter->SetCode(" STYLEREF 1 \\s ");
chapter->GetCharacterFormat()->SetFontSize(9);
chapter->GetCharacterFormat()->SetTextColor(Color::FromArgbRGB(90, 99, 112));
intrusive_ptr<TextRange> dash = caption->AppendText("-");
dash->GetCharacterFormat()->SetFontSize(9);
dash->GetCharacterFormat()->SetTextColor(Color::FromArgbRGB(90, 99, 112));
//The SEQ field generates the caption number; the caption name is "Figure"
intrusive_ptr<SequenceField> seq = Object::Dynamic_cast<SequenceField>(
caption->AppendField("Figure", FieldType::FieldSequence));
seq->SetCaptionName("Figure");
seq->SetNumberFormat(CaptionNumberingFormat::Number);
seq->GetCharacterFormat()->SetFontSize(9);
seq->GetCharacterFormat()->SetTextColor(Color::FromArgbRGB(90, 99, 112));
//Insert the caption paragraph after the paragraph that holds the picture
bodyItems->Insert(pictureIndex + 1, caption);
//Update the STYLEREF and SEQ fields when the document is saved
document->SetIsUpdateFields(true);
document->SaveToFile("Output/ChapterCaptions.docx", FileFormat::Docx);
document->Close();
return 0;
}
The chart in the example is in Chapter 2, "Regional Sales Structure", so the caption reads "Figure 2-1". The chapter number comes from the Heading 1 number, and the sequence number comes from the SEQ field.

The Chapter Number Depends on the Heading Style and Numbering
STYLEREF 1 \s reads a heading that uses the Heading 1 style and carries a number. When the heading does not use that style, or no numbering scheme is set, the chapter number is empty or incorrect. The Heading 1 paragraphs in the sample document apply decimal numbering directly, so the field result is 2.
The Insertion Index Is Calculated over Body Objects
The index of Body->GetChildObjects()->Insert(index, paragraph) is calculated over body objects, and each table and picture paragraph occupies one position. The example records the index of the picture paragraph in the collection and inserts the caption after it. Operating on the body object collection directly avoids a mismatch between the paragraph collection indexes and the body object indexes.
The Character Format of a Caption Is Set Item by Item
A caption paragraph consists of text and fields, and paragraph formatting does not automatically apply to fields. The example sets the font size and color separately for the label text, the STYLEREF field, the hyphen, and the SEQ field. If only paragraph alignment is set, the field result still inherits the body format.
Add Caption Cross-References
A cross-reference consists of a bookmark and fields. The REF field returns the caption text within the bookmark range, and the PAGEREF field returns the page number where the caption is located. The example first adds bookmark start and end markers to the caption paragraph, and then inserts a paragraph containing the two fields into the body.
| Operation | Method |
|---|---|
| Create bookmark markers | new BookmarkStart(document, name), new BookmarkEnd(document, name) |
| Wrap the caption content | Paragraph->GetChildObjects()->Insert(0, start), Add(end) |
| Reference the caption text | FieldType::FieldRef, field code REF bookmark \h |
| Reference the caption page | FieldType::FieldPageRef, field code PAGEREF bookmark \h |
| Update fields | Document->SetIsUpdateFields(true) |
The detailed steps are as follows.
- Create a Document object and load
ChapterCaptions.docxproduced in the previous section. - Traverse the body objects and find the caption paragraph that starts with "Figure ".
- Insert a BookmarkStart at the beginning of the caption paragraph's child objects and add a BookmarkEnd at the end.
- Insert the cross-reference paragraph after the specified paragraph in the "Channel Trends" section.
- Append a REF field and a PAGEREF field, and set their field codes.
- Enable field update and save the document.
The code for adding cross-references 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/Section.h"
#include "Spire/Doc/Body.h"
#include "Spire/Doc/DocumentObject.h"
#include "Spire/Doc/BookmarkStart.h"
#include "Spire/Doc/BookmarkEnd.h"
#include "Spire/Doc/Collections/SectionCollection.h"
#include "Spire/Doc/Collections/DocumentObjectCollection.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/Field.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/Formatting/ParagraphFormat.h"
#include "Spire/Doc/FieldType.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;
using namespace Spire::Doc::Formatting;
int main() {
//Load the document that contains chapter-numbered captions
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("Output/ChapterCaptions.docx");
intrusive_ptr<Section> section = document->GetSections()->GetItem(0);
intrusive_ptr<DocumentObjectCollection> bodyItems = section->GetBody()->GetChildObjects();
//Find the caption paragraph and wrap its content with a bookmark
intrusive_ptr<Paragraph> caption = nullptr;
for (int i = 0; i < bodyItems->GetCount() && caption == nullptr; i++)
{
intrusive_ptr<DocumentObject> item = bodyItems->GetItem(i);
if (!Object::CheckType<Paragraph>(item))
continue;
intrusive_ptr<Paragraph> paragraph = Object::Dynamic_cast<Paragraph>(item);
if (paragraph->GetText().rfind("Figure ", 0) == 0)
{
caption = paragraph;
}
}
if (caption == nullptr)
{
cout << "Caption paragraph not found" << endl;
document->Close();
return 1;
}
caption->GetChildObjects()->Insert(0, new BookmarkStart(document, "Figure_2_1"));
caption->GetChildObjects()->Add(new BookmarkEnd(document, "Figure_2_1"));
//Find the body paragraph in the "Channel Trends" section
int insertIndex = -1;
for (int i = 0; i < bodyItems->GetCount() && insertIndex < 0; i++)
{
intrusive_ptr<DocumentObject> item = bodyItems->GetItem(i);
if (!Object::CheckType<Paragraph>(item))
continue;
intrusive_ptr<Paragraph> paragraph = Object::Dynamic_cast<Paragraph>(item);
if (paragraph->GetText().find("core business districts still dominate sales") != string::npos)
insertIndex = i + 1;
}
if (insertIndex < 0)
{
cout << "Insertion position not found" << endl;
document->Close();
return 1;
}
//Build the cross-reference paragraph: REF shows the caption text, PAGEREF shows the page
intrusive_ptr<Paragraph> reference = new Paragraph(document);
reference->GetFormat()->SetAfterSpacing(4);
reference->AppendText("See ");
intrusive_ptr<Field> ref = reference->AppendField("Figure", FieldType::FieldRef);
ref->SetCode(" REF Figure_2_1 \\h ");
reference->AppendText(" (page ");
intrusive_ptr<Field> pageRef = reference->AppendField("1", FieldType::FieldPageRef);
pageRef->SetCode(" PAGEREF Figure_2_1 \\h ");
reference->AppendText(") for a comparison of regional sales; the reference updates automatically when the caption number changes.");
bodyItems->Insert(insertIndex, reference);
//Update the REF and PAGEREF fields when the document is saved
document->SetIsUpdateFields(true);
document->SaveToFile("Output/CrossReference.docx", FileFormat::Docx);
document->Close();
return 0;
}
The body shows "See Figure 2-1 (page 1) for a comparison of regional sales; the reference updates automatically when the caption number changes." The caption text comes from the bookmark range, and the page number is calculated by the PAGEREF field.

The Roles of REF and PAGEREF
REF Figure_2_1 \h shows the text within the bookmark range, which in this example is the caption number "Figure 2-1". PAGEREF Figure_2_1 \h shows the page where the bookmark is located. The \h switch turns the field result into a hyperlink for navigation within the document.
The Bookmark Range Determines the Referenced Content
The example inserts the bookmark start marker before the first child object of the caption paragraph and appends the end marker to the end of the paragraph, so the bookmark covers the entire caption text. If only the caption number is wrapped, REF shows only the number; if the whole paragraph is wrapped, REF shows the description text as well.
Fields Must Be Updated After Caption Numbers Change
A cross-reference stores field codes, and the displayed result is produced by field update. After captions are renumbered or moved to another page, the fields in the document should be updated. The example calls SetIsUpdateFields(true) before saving, so the REF and PAGEREF fields are updated together when the document is saved.
Captions and cross-references both depend on fields. AddCaption() is suitable for generating standard captions quickly; when chapter numbers or custom numbering are required, combine STYLEREF and SEQ fields; a cross-reference locates the caption with a bookmark and reads the number and page through REF and PAGEREF fields. After a caption is created or modified, the fields should be updated to keep the numbers and reference results consistent.
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
