DocumentNavigator exposes the character formatting, paragraph formatting, page setup, list formatting and cell formatting of the current position while locating the cursor. A formatting object applies to the text, paragraph or section where the cursor is located, and content written afterwards adopts the current formatting directly. A table is built through the call order of starting the table, inserting cells, ending the row and ending the table. After the table is built, cells can be located to modify their content and shading, or a specified row can be deleted.
Spire.Doc for C++ sets formatting through GetCharacterFormat(), GetParagraphFormat(), GetPageSetup() and GetListFormat(), and works with tables through StartTable(), InsertCell(), EndRow(), EndTable(), MoveToCell() and DeleteRow(). All processing is completed in the local process, and the document does not need to be uploaded to any service.
This article covers two features:
The example uses the "Smart Warehousing Management Platform Project Brief" (ProjectBrief.docx). It appends formatting examples and a deliverables list to the end of the document, and builds an implementation schedule table. The formatting and table methods provided by the navigator are listed below:
| Operation | Method |
|---|---|
| Character formatting | GetCharacterFormat() |
| Paragraph formatting | GetParagraphFormat() |
| Page setup | GetPageSetup() |
| List formatting | GetListFormat() |
| Save and restore character formatting | PushCharacterFormat() / PopCharacterFormat() |
| Start a table | StartTable() |
| Insert a cell | InsertCell() |
| End the current row | EndRow() |
| End the table | EndTable() |
| Locate a cell | MoveToCell(tableIndex, rowIndex, columnIndex, characterIndex) |
| Cell formatting | GetCellFormat() |
| Delete a row | DeleteRow(tableIndex, rowIndex) |
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.
Set Character, Paragraph, Page and List Formatting
GetCharacterFormat() sets character attributes such as font, font size, bold, underline and color; GetParagraphFormat() sets line spacing, indentation and alignment; GetPageSetup() sets the paper size and page margins; and GetListFormat() applies bullets or numbering. A formatting object applies to the position where the cursor is located, and text written afterwards adopts the current settings.
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Create a DocumentNavigator object and start a new paragraph at the end of the document.
- Call GetCharacterFormat() to set bold, single underline and blue, and write text.
- Call PushCharacterFormat() to save the formatting, call ClearFormatting() to clear it and write default text, and then call PopCharacterFormat() to restore it.
- Call MoveToParagraph(2, 0) to position the third paragraph, and set 1.75 line spacing and a left indent.
- Call GetPageSetup() to set the paper to B5 and to set the page margins.
- Write the deliverables list at the end of the document, and call GetListFormat()->ApplyBulletStyle() to apply bullets to the three items.
- Save the result document with Document->SaveToFile().
The code for setting formatting 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/PageSetup.h"
#include "Spire/Doc/Documents/DocumentNavigator.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Documents/PageSize.h"
#include "Spire/Doc/Documents/MarginsF.h"
#include "Spire/Doc/Documents/UnderlineStyle.h"
#include "Spire/Doc/LineSpacingRule.h"
#include "Spire/Doc/Formatting/CharacterFormat.h"
#include "Spire/Doc/Formatting/ParagraphFormat.h"
#include "Spire/Doc/Formatting/ListFormat.h"
#include "Spire/Doc/Collections/ParagraphCollection.h"
#include "Spire/Doc/common/h/Color.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Formatting;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Common;
int main() {
//Load the project brief document
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ProjectBrief.docx");
intrusive_ptr<DocumentNavigator> navigator = new DocumentNavigator(document);
//Move to the end of the document and start a new paragraph
navigator->MoveToDocumentEnd();
navigator->Writeln();
//Set the character formatting, then write the text
navigator->GetCharacterFormat()->SetBold(true);
navigator->GetCharacterFormat()->SetUnderlineStyle(UnderlineStyle::Single);
navigator->GetCharacterFormat()->SetTextColor(Color::GetBlue());
navigator->Write("This text is bold, underlined and blue.");
navigator->Writeln();
//Use the format stack to restore the default formatting temporarily
navigator->PushCharacterFormat();
navigator->GetCharacterFormat()->ClearFormatting();
navigator->Write("This text is written after clearing the formatting.");
navigator->PopCharacterFormat();
navigator->Writeln();
//Clear the character formatting restored by the format stack so the list does not inherit blue bold
navigator->GetCharacterFormat()->ClearFormatting();
//Set the line spacing and left indent of the third paragraph
navigator->MoveToParagraph(2, 0);
navigator->GetParagraphFormat()->SetLineSpacingRule(LineSpacingRule::Multiple);
navigator->GetParagraphFormat()->SetLineSpacing(1.75 * 12);
navigator->GetParagraphFormat()->SetLeftIndent(18);
//Set the paper size and page margins
navigator->GetPageSetup()->SetPageSize(PageSize::B5());
navigator->GetPageSetup()->SetMargins(new MarginsF(70, 55, 70, 55));
//Write the deliverables list at the end of the document
int count = document->GetLastSection()->GetParagraphs()->GetCount();
navigator->MoveToDocumentEnd();
navigator->Writeln();
navigator->Writeln("Deliverables:");
navigator->Writeln("Requirements Specification");
navigator->Writeln("Deployment and Interface Integration Report");
navigator->Writeln("Trial Operation Record and Acceptance Confirmation");
//Restore the list title to no indent
navigator->MoveToParagraph(count, 0);
navigator->GetParagraphFormat()->SetFirstLineIndent(0);
navigator->GetParagraphFormat()->SetLeftIndent(0);
//Apply bullet style to the three items in the list
for (int i = count + 1; i <= count + 3; i++)
{
navigator->MoveToParagraph(i, 0);
navigator->GetListFormat()->ApplyBulletStyle();
}
//Save the document as docx
document->SaveToFile("Output/FormatText.docx", FileFormat::Docx);
document->Close();
return 0;
}
After the formatting is applied, the bold, underlined and blue text contrasts with the text written after clearing the formatting, and the deliverables appear as a bulleted list:

The third paragraph has wider line spacing and a larger left indent, and the page is changed to B5 paper with smaller margins:

Character formatting applies to text written after the cursor
GetCharacterFormat() returns the character formatting of the current cursor position. Changes take effect on the text written afterwards and do not alter text that already exists. The example sets bold, single underline and blue first and then calls Write(), so only the newly written sentence carries these attributes. To write several styles within one paragraph, set the formatting again before each write.
The Push/Pop format stack switches formatting temporarily
PushCharacterFormat() saves the current character formatting, and PopCharacterFormat() restores the saved formatting. The example saves the formatting after setting bold and blue, calls ClearFormatting() to clear it and writes default text, and then restores the formatting. The restored formatting continues to apply to text written afterwards, so the example calls ClearFormatting() again before writing the list, preventing the list title and items from inheriting blue bold.
Scope of paragraph formatting and page setup
GetParagraphFormat() applies to the paragraph where the cursor is located, and GetPageSetup() applies to the current section. The example first calls MoveToParagraph(2, 0) to position the third paragraph and then sets the line spacing and indent, leaving other paragraphs unaffected. Page setup changes the paper and margins of the whole section. The example changes A4 to B5 and sets the margins to 70, 55, 70 and 55 points, and the body text is laid out again according to the new page width.
List formatting is applied paragraph by paragraph
GetListFormat()->ApplyBulletStyle() applies to the paragraph where the cursor is located and does not extend to adjacent paragraphs automatically. The example writes the list title and the three items first, and then uses MoveToParagraph() to locate each item and apply bullets. The paragraph indices are obtained through GetLastSection()->GetParagraphs()->GetCount() before writing, and relative positions are calculated after writing, which avoids hard-coded indices.
Build and Modify a Table
A table is started with StartTable(), cells are added in order with InsertCell(), the current row is ended with EndRow(), and the table is ended with EndTable(). After the table is built, MoveToCell() locates a specified cell, GetCellFormat() modifies shading and alignment, and DeleteRow() deletes a specified row.
The detailed steps are as follows.
- Create a Document object and call LoadFromFile() to load the sample document.
- Create a DocumentNavigator object, start a new paragraph at the end of the document, and write the table title.
- Call StartTable() to start the table, call InsertCell() to fill the header row, set bold white text and cell alignment, and call EndRow() to end the header row.
- Continue calling InsertCell() and EndRow() to fill three data rows, and call EndTable() to end the table.
- Apply dark blue shading to the header cells.
- Call MoveToCell(0, 2, 1, 0) to locate row 3, column 2, write the owner and set the cell shading.
- Print the row count, call DeleteRow(0, 3) to delete the fourth row, and print the row count again.
- Save the result document with Document->SaveToFile().
The code for building and modifying the 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/Table.h"
#include "Spire/Doc/TableRow.h"
#include "Spire/Doc/TableCell.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/Documents/DocumentNavigator.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/Formatting/CellFormat.h"
#include "Spire/Doc/Formatting/CharacterFormat.h"
#include "Spire/Doc/Formatting/ParagraphFormat.h"
#include "Spire/Doc/Fields/Shapes/Shading.h"
#include "Spire/Doc/Collections/RowCollection.h"
#include "Spire/Doc/Collections/CellCollection.h"
#include "Spire/Doc/Collections/ParagraphCollection.h"
#include "Spire/Doc/LineSpacingRule.h"
#include "Spire/Doc/Documents/VerticalAlignment.h"
#include "Spire/Doc/common/h/Color.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;
using namespace Spire::Doc::Formatting;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Common;
// Set compact line spacing and paragraph spacing for a cell paragraph
static void compact(intrusive_ptr<Paragraph> paragraph)
{
paragraph->GetFormat()->SetBeforeSpacing(0);
paragraph->GetFormat()->SetAfterSpacing(0);
paragraph->GetFormat()->SetLineSpacingRule(LineSpacingRule::Multiple);
paragraph->GetFormat()->SetLineSpacing(12);
}
int main() {
//Load the project brief document
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("ProjectBrief.docx");
intrusive_ptr<DocumentNavigator> navigator = new DocumentNavigator(document);
//Move to the end of the document and start a new paragraph
navigator->MoveToDocumentEnd();
navigator->Writeln();
navigator->Write("Implementation Schedule:");
navigator->Writeln();
//Start the table and fill the header row; the cell already has a default paragraph, so use it directly
intrusive_ptr<Table> table = navigator->StartTable();
const char* headers[3] = {"Phase", "Owner", "Planned Duration"};
for (int c = 0; c < 3; c++)
{
intrusive_ptr<TableCell> cell = navigator->InsertCell();
intrusive_ptr<Paragraph> paragraph = cell->GetParagraphs()->GetItem(0);
compact(paragraph);
intrusive_ptr<TextRange> range = paragraph->AppendText(headers[c]);
range->GetCharacterFormat()->SetBold(true);
range->GetCharacterFormat()->SetTextColor(Color::GetWhite());
cell->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
}
navigator->EndRow();
//Fill the data rows; leave the owner of System Deployment blank for now
const char* rows[3][3] = {
{"Project Kickoff", "Zhang", "4 weeks"},
{"System Deployment", "", "8 weeks"},
{"Trial Operation", "Wang", "6 weeks"}};
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
intrusive_ptr<TableCell> cell = navigator->InsertCell();
intrusive_ptr<Paragraph> paragraph = cell->GetParagraphs()->GetItem(0);
compact(paragraph);
paragraph->AppendText(rows[r][c]);
cell->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
}
navigator->EndRow();
}
navigator->EndTable();
//After the table is built, apply dark blue shading to the header cells
for (int c = 0; c < 3; c++)
{
table->GetRows()->GetItem(0)->GetCells()->GetItem(c)
->GetCellFormat()->GetShading()->SetBackgroundPatternColor(
Color::FromArgbRGB(31, 56, 100));
}
//Move to row 3, column 2, write the owner and set the shading
navigator->MoveToCell(0, 2, 1, 0);
navigator->Write("Li");
navigator->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
navigator->GetCellFormat()->GetShading()->SetBackgroundPatternColor(
Color::GetCadetBlue());
//Delete the fourth row and print the change in row count
cout << "Rows before deletion: " << table->GetRows()->GetCount() << endl;
navigator->DeleteRow(0, 3);
cout << "Rows after deletion: " << table->GetRows()->GetCount() << endl;
//Save the document as docx
document->SaveToFile("Output/TableNavigator.docx", FileFormat::Docx);
document->Close();
return 0;
}
The console output is shown below:
Rows before deletion: 4
Rows after deletion: 3
The table finally keeps the header row and two data rows. The owner cell of "System Deployment" contains "Li" and is displayed with light blue shading:

A table advances row by row
StartTable() starts a new table, InsertCell() adds cells to the current row in order, EndRow() ends the current row and begins the next row, and EndTable() ends the table. The number of cells inserted in the first row determines the number of columns, and subsequent rows should insert the same number of cells. The example inserts three header cells and ends the row, and then fills three data rows with three columns.
A cell already contains a default paragraph
The cell returned by InsertCell() already contains a default paragraph. The code obtains this paragraph through cell->GetParagraphs()->GetItem(0) and writes text into it directly, without calling AddParagraph(). If an extra paragraph is added, an empty paragraph remains in the cell and the row height increases accordingly. The spacing and line spacing of the cell paragraph can be adjusted through GetFormat(). The example uses a helper function to set the before and after spacing to 0 and the line spacing to 12 points.
Shading should be set after the table is built
InsertCell() inherits the formatting of the preceding cell. If dark blue shading is applied immediately while the header row is filled, the data cells inserted afterwards inherit the same fill color, and the whole table is displayed in dark blue. The example completes the text filling of all rows and columns first, and then applies shading to the header cells as a whole; afterwards, MoveToCell() and GetCellFormat() modify only the shading of the target cell, leaving other cells unaffected.
Indices of MoveToCell and DeleteRow
All four parameters of MoveToCell(tableIndex, rowIndex, columnIndex, characterIndex) start from 0. tableIndex is the order of the table in the body, rowIndex and columnIndex are the row and column indices within the table, and characterIndex is the character position within the cell. The example locates row 3, column 2 and writes text. DeleteRow(tableIndex, rowIndex) deletes the specified row, and the rows after it move up in order; after the example deletes the fourth row, the row count changes from 4 to 3.
Apply for a Temporary License
If you want to remove the evaluation message from the result document or lift the feature limitations, request a 30-day trial license.
