Knowledgebase (2311)
Children categories
If you want to make your paragraphs easier to navigate and read, rearranging them into alphabetical, numerical, or even bullet order will allow your readers to quickly find what they are looking for and search through the list in an instant. In this article, you will learn how to create numbered lists, bulleted lists, and multilevel lists in a Word document in C++ using Spire.Doc for C++.
- Create a Numbered List in Word in C++
- Create a Bulleted List in Word in C++
- Create a Multilevel Numbered List in Word in C++
- Create a Multilevel Mixed-Type List in Word in C++
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, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.Doc for C++ in a C++ Application
Create a Numbered List in Word in C++
Spire.Doc for C++ offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph->GetListFormat()->ApplyStyle() method. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document->AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type using ListLevel->SetPatternType() method.
- Add the list style to the document using Document->GetListStyles()->Add() method.
- Add several paragraphs to the document using Section->AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
- Specify the list level using Paragraph->GetListFormat()->GetListLevelNumber() method.
- Save the document to a Word file using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Add a section
intrusive_ptr<Section> section = document->AddSection();
//Create a numbered list style
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
listStyle->SetName(L"numberedList");
listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedParen);
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetListStyles()->Add(listStyle);
//Add a paragraph
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"Required Web Development Skills:");
paragraph->GetFormat()->SetAfterSpacing(5);
//Add a paragraph and apply the numbered list style to it
paragraph = section->AddParagraph();
paragraph->AppendText(L"HTML");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Add another four paragraphs and apply the numbered list style to them
paragraph = section->AddParagraph();
paragraph->AppendText(L"CSS");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"C++Script");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"Python");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"MySQL");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Save the document to file
document->SaveToFile(L"output/NumberedList.docx", FileFormat::Docx2019);
document->Dispose();
}

Create a Bulleted List in Word in C++
The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document->AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list using ListStyle->GetLevels()->Get(index) method, and set the bullet symbol using ListLevel->SetBulletCharacter() method.
- Add the list style to the document using Document->GetListStyles()->Add() method.
- Add several paragraphs to the document using Section->AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
- Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
- Save the document to a Word file using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Add a section
intrusive_ptr<Section> section = document->AddSection();
//Create a bulleted list style
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Bulleted);
listStyle->SetName(L"bulletedList");
listStyle->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7");
listStyle->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol");
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetListStyles()->Add(listStyle);
//Add a paragraph
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"Computer Science Subjects:");
paragraph->GetFormat()->SetAfterSpacing(5);
//Add a paragraph and apply the bulleted list style to it
paragraph = section->AddParagraph();
paragraph->AppendText(L"Data Structure");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Add another five paragraphs and apply the bulleted list style to them
paragraph = section->AddParagraph();
paragraph->AppendText(L"Algorithm");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"Computer Networks");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"Operating System");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"C Programming");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"Theory of Computations");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Save the document to file
document->SaveToFile(L"output/BulletedList.docx", FileFormat::Docx2019);
document->Dispose();
}

Create a Multilevel Numbered List in Word in C++
A multilevel list consists of at least two different levels. Each level of a nested list can be accessed using ListStyle->GetLevels()->GetItem(index) method. Through ListLevel object, you can set the numbering type and prefix for a certain level. The following are the steps to create a multilevel numbered list in Word.
- Create a Document object.
- Add a section using Document->AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type and prefix.
- Add the list style to the document using Document->GetListStyles()->Add() method.
- Add several paragraphs to the document using Section->AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
- Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
- Save the document to a Word file using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Add a section
intrusive_ptr<Section> section = document->AddSection();
//Create a numbered list style, specifying number prefix and pattern type of each level
intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
listStyle->SetName(L"nestedStyle");
listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
listStyle->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1.");
listStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic);
listStyle->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2.");
listStyle->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic);
document->GetListStyles()->Add(listStyle);
//Add a paragraph
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"Here's a Multi-Level Numbered List:");
paragraph->GetFormat()->SetAfterSpacing(5);
//Add a paragraph and apply the numbered list style to it
paragraph = section->AddParagraph();
paragraph->AppendText(L"The first item");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Add another five paragraphs and apply the numbered list stype to them
paragraph = section->AddParagraph();
paragraph->AppendText(L"The second item");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The first sub-item");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The second sub-item");
paragraph->GetListFormat()->ContinueListNumbering();
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph = section->AddParagraph();
paragraph->AppendText(L"A sub-sub-item");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The third item");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Save the document to file
document->SaveToFile(L"output/MultilevelNumberedList.docx", FileFormat::Docx2019);
document->Dispose();
}

Create a Multilevel Mixed-Type List in Word in C++
A multilevel list can be a combination of numbered lists and bulleted lists. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document->AddSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section->AddParagraph() method.
- Apply different list style to different paragraphs using Paragraph->GgetListFormat()->ApplyStyle() method.
- Save the document to a Word file using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Add a section
intrusive_ptr<Section> section = document->AddSection();
//Create a numbered list style
intrusive_ptr<ListStyle> numberedListStyle = new ListStyle(document, ListType::Numbered);
numberedListStyle->SetName(L"numberedStyle");
numberedListStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
numberedListStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
numberedListStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter);
document->GetListStyles()->Add(numberedListStyle);
//Create a bulleted list style
intrusive_ptr<ListStyle> bulletedListStyle = new ListStyle(document, ListType::Bulleted);
bulletedListStyle->SetName(L"bulletedStyle");
bulletedListStyle->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A");
bulletedListStyle->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol");
document->GetListStyles()->Add(bulletedListStyle);
//Add a paragraph
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"Here's a Multi-Level Mixed List:");
paragraph->GetFormat()->SetAfterSpacing(5);
//Add a paragraph and apply the numbered list style to it
paragraph = section->AddParagraph();
paragraph->AppendText(L"The first item");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Add another five paragraphs and apply different list stype to them
paragraph = section->AddParagraph();
paragraph->AppendText(L"The first sub-item");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The second sub-item");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph = section->AddParagraph();
paragraph->AppendText(L"The first sub-sub-item");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The second sub-sub-item");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"The second item");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//Save the document to file
document->SaveToFile(L"output/MultilevelMixedList.docx", FileFormat::Docx);
document->Dispose();
}

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.
Spire.Doc for Python is a professional Word Python API specifically designed for developers to create, read, write, convert and compare Word documents with fast and high-quality performance.
As an independent Word Python API, Spire.Doc for Python doesn't need Microsoft Word to be installed on neither the development nor target systems. However, it can incorporate Microsoft Word document creation capabilities into any developers' Python applications.
Inserting images into a document is a great way to enhance its visual appearance and make it more understandable for readers. For example, if you are creating a product guide for a piece of furniture that has complex assembly steps, including images of each step can help users understand how to assemble the product quickly. In this article, you will learn how to insert images into PDF documents along with how to replace and delete images in PDF documents in C++ using Spire.PDF for C++.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete an Image from a PDF Document
Install Spire.PDF for C++
There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.PDF for C++ in a C++ Application
Insert an Image into a PDF Document in C++
Spire.PDF for C++ offers the PdfPageBase->GetCanvas()->DrawImage(intrusive_ptr<PdfImage> image, float x, float y, float width, float height) method to add an image to a specific page in a PDF document. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Initialize an instance of the PdfImage class.
- Load an image using the PdfImage->FromFile(LPCWSTR_S filename) method.
- Draw the image to a specific location on the page using the PdfPageBase->GetCanvas()->DrawImage(intrusive_ptr<PdfImage> image, float x, float y, float width, float height) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf->LoadFromFile(L"Input.pdf");
//Get the first page of the PDF file
intrusive_ptr<PdfPageBase> page = pdf->GetPages()->GetItem(0);
//Initialize an instance of the PdfImage class
intrusive_ptr<PdfImage> image = new PdfImage();
//Load an image
image = image->FromFile(L"PDF-CPP.png");
float width = image->GetWidth() * 0.5;
float height = image->GetHeight() * 0.5;
float x = (page->GetCanvas()->GetClientSize()->GetWidth() - width) / 2;
//Draw the image to a specific location on the page
page->GetCanvas()->DrawImage(image, x, 120, width, height);
//Save the result file
pdf->SaveToFile(L"AddImage.pdf");
pdf->Close();
}

Replace an Image with Another Image in a PDF Document in C++
You can use the PdfImageHelper->ReplaceImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo,intrusive_ptr<PdfImage>image) method to replace an existing image on a PDF page with another image. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Create an instance of the PdfImageHelper class.
- Get images of the page using the PdfImageHelper->GetImagesInfo(intrusive_ptr<PdfPageBase> page) method.
- Initialize an instance of the PdfImage class.
- Load an image using the PdfImage->FromFile(LPCWSTR_S filename) method.
- Replace a specific image on the page with the loaded image using the PdfImageHelper->ReplaceImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo,intrusive_ptr<PdfImage>image) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptrPdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf->LoadFromFile(L"AddImage.pdf");
//Get the first page of the PDF file
intrusive_ptrPdfPageBase> page = pdf->GetPages()->GetItem(0);
//Create a PdfImageHelper object
intrusive_ptrPdfImageHelper> imagehelper = new PdfImageHelper();
//Get image information from the page
std::vector<intrusive_ptr<Utilities_PdfImageInfo> > imageInfo = imagehelper->GetImagesInfo(page);
//Initialize an instance of the PdfImage class
intrusive_ptrPdfImage> image = new PdfImage();
//Load an image
image = image->FromFile(L"PDF-java.png");
//Replace the first image on the first page
imagehelper->ReplaceImage(imageInfo[0], image);
//Save the result file
pdf->SaveToFile(L"ReplaceImage.pdf");
pdf->Close();
}

Delete an Image from a PDF Document in C++
You can use the PdfImageHelper->DeleteImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo) method to delete a specific image from a PDF page. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Create an instance of the PdfImageHelper class.
- Get images of the page using the PdfImageHelper->GetImagesInfo(intrusive_ptr<PdfPageBase> page) method.
- Delete a specific image on the page using the PdfImageHelper->DeleteImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptrPdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf->LoadFromFile(L"AddImage.pdf");
//Get the first page of the PDF file
intrusive_ptrPdfPageBase> page = pdf->GetPages()->GetItem(0);
//Create a PdfImageHelper object
intrusive_ptrPdfImageHelper> imagehelper = new PdfImageHelper();
//Get image information from the page
std::vector<intrusive_ptr<Utilities_PdfImageInfo>> imageInfo = imagehelper->GetImagesInfo(page);
//Delete the first image on the first page
imagehelper->DeleteImage(imageInfo[0]);
//Save the result file
pdf->SaveToFile(L"DeleteImage.pdf");
pdf->Close();
}

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.


