Knowledgebase (2345)
Children categories
In MS Word, a Page Break is an important feature of page layout that helps you start a new page wherever you want. After inserting page breaks, all formatting of the previous page applies to the new page, which makes the whole document neat and well organized. In this article, you will learn how to programmatically add or remove page breaks in a Word document using Spire.Doc for C++.
- Insert a Page Break after a Specific Paragraph in Word in C++
- Insert a Page Break after a Specific Text in Word in C++
- Remove Page Breaks in a Word Document 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
Insert a Page Break after a Specific Paragraph in Word in C++
Spire.Doc for C++ offers the Paragraph->AppendBreak(BreakType::PageBreak) method to insert a page break after a paragraph. Once inserted, a symbol indicating the page break will be shown. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Get a specified section using Document->GetSections()->GetItem(sectionIndex) method.
- Get a specified paragraph using Section->GetParagraphs()->GetItem(paragraphIndex) method.
- Add a page break to end of the paragraph using Paragraph->AppendBreak(BreakType::PageBreak) method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"Input.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"InsertPageBreak.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Get the first section
intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);
//Get the 2nd paragraph in the section
intrusive_ptr <Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(1);
//Insert a page break after the paragraph
paragraph->AppendBreak(BreakType::PageBreak);
//Save the result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->Close();
}

Insert a Page Break after a Specific Text in Word in C++
In addition to inserting a page break after a paragraph, Spire.Doc for C++ also allows you to find a specified text and then insert the page break after the specified text. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Find a specified text using Document->FindString() method.
- Get the text range of the specified text using TextSelection->GetAsOneRange() method.
- Get the paragraph where the text range is located using TextRange->GetOwnerParagraph() method.
- Get the position index of the text range in the paragraph using Paragraph->GetChildObjects()->IndexOf() method.
- Initialize an instance of Break class to create a page break.
- Insert the page break after the specified text using Paragraph->GetChildObjects()->Insert() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"Input.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"InsertPageBreakAfterText.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Find a specified text
intrusive_ptr<TextSelection> selection = document->FindString(L"infrastructure", true, true);
//Get the text range of the specified text
intrusive_ptr<TextRange> range = selection->GetAsOneRange();
//Get the paragraph where the text range is located
intrusive_ptr<Paragraph> paragraph = range->GetOwnerParagraph();
//Get the position index of the text range in the paragraph
int index = paragraph->GetChildObjects()->IndexOf(range);
//Create a page break
intrusive_ptr<Break> pageBreak = new Break(document, BreakType::PageBreak);
//Insert a page break after the specified text
paragraph->GetChildObjects()->Insert(index + 1, pageBreak);
//Save to result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->Close();
}

Remove Page Breaks in a Word Document in C++
Some mistakenly added page breaks can mess up the structure of your entire document, so it's quite necessary to remove them. The following are the steps to remove page breaks in a Word document.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Traverse through each paragraph in the first section, and then traverse through each child object of a paragraph.
- Determine whether the child object type is a page break. If yes, remove the page break from the paragraph using Paragraph->GetChildObjects()->Remove() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"InsertPageBreak.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"RemovePageBreaks.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Traverse through each paragraph in the first section of the document
for (int j = 0; j < document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetCount(); j++)
{
intrusive_ptr<Paragraph> p = document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetItemInParagraphCollection(j);
//Traverse through each child object of a paragraph
for (int i = 0; i < p->GetChildObjects()->GetCount(); i++)
{
intrusive_ptr <DocumentObject> obj = p->GetChildObjects()->GetItem(i);
//Determine whether the child object type is a page break
if (Object::CheckType<Break>(obj) && (Object::Dynamic_cast<Break>(obj))->GetBreakType() == BreakType::PageBreak)
{
//Remove the page break from the paragraph
p->GetChildObjects()->Remove(obj);
}
}
}
//Save the result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->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.
PDF is one of the most versatile, universal and feature-rich file formats in use today. Compared to Word, PDF documents are less likely to lose formatting when they’re opened on various devices. Additionally, PDF has absolute advantages over Word in terms of document security, archiving and transmission. These are some of the reasons why we should convert Word to PDF. In this article, you will learn how to convert Word to PDF and how to set conversion options in C++ using Spire.Doc for C++.
- Convert Doc or Docx to PDF
- Convert Word to PDF with Bookmarks
- Convert Word to PDF with Fonts Embedded
- Set Image Quality When Converting Word to PDF
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
Convert Doc or Docx to PDF in C++
The Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method provided by Spire.Doc for C++ allows to save Word as PDF, XPS, HTML, RTF, etc. If you just want to save your Word documents as regular PDFs without additional settings, follow the steps below.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Save the document to PDF using Doucment->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->Close();
}

Convert Word to PDF with Bookmarks in C++
Bookmarks can enhance the readability of a document. When generating PDF from Word, you may would like to preserve existing bookmarks of the Word document or create bookmarks from the headings. The following are the steps to convert Word to PDF with bookmarks.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Create a ToPdfParameterList object, which is used to set conversion options.
- Create bookmarks in PDF from the headings in Word using ToPdfParameterList.SetCreateWordBookmarksUsingHeadings() method.
- Save the document to PDF with bookmarks using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Create a ToPdfParameterList object
intrusive_ptr<ToPdfParameterList> parameters = new ToPdfParameterList();
//Create bookmarks from Word headings
parameters->SetCreateWordBookmarksUsingHeadings(true);
//Create bookmarks in PDF from existing bookmarks in Word
//parameters->SetCreateWordBookmarks(true);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
}

Convert Word to PDF with Fonts Embedded in C++
By embedding fonts used in a Word document into the PDF document, you ensure that the PDF document looks the same on any device that does not have the appropriate fonts installed. The steps to embed fonts in PDF during conversion are as follows.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Create a ToPdfParameterList object, which is used to set conversion options.
- Embed fonts in generated PDF using ToPdfParameterList.SetIsEmbeddedAllFonts() method.
- Save the document to PDF using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Create a ToPdfParameterList object
intrusive_ptr<ToPdfParameterList> parameters = new ToPdfParameterList();
//Embed fonts used in Word in generated PDF
parameters->SetIsEmbeddedAllFonts(true);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
}

Set Image Quality When Converting Word to PDF in C++
A document containing a large number of high-quality images will often be large in size. When you convert Word to PDF, you can decide whether to compress the image quality or not. The following are the detailed steps.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Set the image quality using Document->SetJPEGQuality() mehtod
- Save the document to PDF using Doucment->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Compress image to 40% of the original quality
document->SetJPEGQuality(40);
//Preserve original image quality
//document->SetJPEGQuality(100);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->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.
Extracting text and images is a common requirement while working with Word documents. This will help you to save useful content out of the original document to re-use in a new document or for other purposes. In this article, you will learn how to extract text or images from a Word document using Spire.Doc for 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
Extract Text from a Word Document in C++
To extract the text content from an existing Word document, Spire.Doc for C++ provides the Document->GetText() method. The following are steps to extract text and save in a TXT file.
- Create a Document instance.
- Load a sample Word document using Document->LoadFromFile() method.
- Get text from the document using Document->GetText() method.
- Create a new txt file and write the extracted text to the file.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring data_path = L"Data\\";
std::wstring inputFile = data_path + L"input.docx";
//Specify output file path and name
std::wstring outputPath = L"Output\\";
std::wstring outputFile = outputPath + L"GetText.txt";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a sample Word document from disk
document->LoadFromFile(inputFile.c_str());
//Get text from the document
std::wstring text = document->GetText();
//Create a new TXT File to save the extracted text
std::wofstream write(outputFile);
write << text;
write.close();
document->Close();
}

Extract Images from a Word Document in C++
For a Word document with a lot of images, manually saving these images one by one is quite time-consuming. Below are steps to extract all images at once using Spire.Doc for C++.
- Load a sample Word document using Document->LoadFromFile() method.
- Append the document to the end of the deque, and then create a vector of images list.
- Traverse through all child objects of the document.
- Determine whether the object type is picture. If yes, get each image using DocPicture->GetImage() method and add it to the list.
- Save the extracted images out of the document in a specified output file path.
- C++
#include "Spire.Doc.o.h"
#include <deque>
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring data_path = L"Data\\";
std::wstring inputFile = data_path + L"input.docx";
//Specify output file path and name
std::wstring outputPath = L"Output\\";
std::wstring outputFile = outputPath + L"ExtractImage/";
//Load a sample Word document
intrusive_ptr<Document> document = new Document();
document->LoadFromFile(inputFile.c_str());
//Append the document to the end of the deque
std::deque<intrusive_ptr<ICompositeObject>> nodes;
nodes.push_back(document);
//Create a vector of images list
std::vector<std::vector<byte>> images;
//Traverse through all child objects of the document
while (nodes.size() > 0)
{
intrusive_ptr<ICompositeObject> node = nodes.front();
nodes.pop_front();
for (int i = 0; i < node->GetChildObjects()->GetCount(); i++)
{
intrusive_ptr<IDocumentObject> child = node->GetChildObjects()->GetItem(i);
if (child->GetDocumentObjectType() == DocumentObjectType::Picture)
{
intrusive_ptr<DocPicture> picture = Object::Dynamic_cast<DocPicture>(child);
std::vector<byte> imageByte = picture->GetImageBytes();
images.push_back(imageByte);
}
else if (Object::CheckType<ICompositeObject>(child))
{
nodes.push_back(boost::dynamic_pointer_cast<ICompositeObject>(child));
}
}
}
//Save the images out of the document
for (size_t i = 0; i < images.size(); i++)
{
std::wstring fileName = L"Image-" + std::to_wstring(i) + L".png";
std::ofstream outFile(fileName, std::ios::binary);
if (outFile.is_open())
{
outFile.write(reinterpret_cast<const char*>(images[i].data()), images[i].size());
outFile.close();
}
}
document->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.