Knowledgebase (2345)
Children categories
When you are handling a PDF document with lots of pages, you will most likely need to add new pages to include more information or remove some redundant pages. In this article, you will learn how to programmatically add or delete pages in a PDF document using Spire.PDF for C++.
- Add a Blank Page at the End of a PDF Document in C++
- Insert a Blank Page After a Specific Page in PDF in C++
- Delete an Existing Page in a PDF Document in C++
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
Add a Blank Page at the End of a PDF Document in C++
Spire.PDF for C++ allows you to add an empty page with specific size and margins at the end of the document using PdfDocument->GetPages()->Add(SizeF* size, PdfMargins* margins) method. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument->LoadFromFile() method.
- Create an empty page with the specified size and margins and then append it to the end of the document using PdfDocument->GetPages()->Add(SizeF* size, PdfMargins* margins) method.
- Save the result document using PdfDocument->SaveToFile () method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input and output file paths
wstring inputFile = L"Data\\input.pdf";
wstring outputFile = L"Output\\InsertEmptyPageAtEnd.pdf";
//Create a PdfDocument object
PdfDocument* pdf = new PdfDocument();
//Load a PDF document
pdf->LoadFromFile(inputFile.c_str());
//Add an empty page at the end of the document
PdfMargins tempVar(0, 0);
pdf->GetPages()->Add(new SizeF(PdfPageSize::A4()), &tempVar);
//Save the result document
pdf->SaveToFile(outputFile.c_str());
pdf->Close();
delete pdf;
}

Insert a Blank Page After a Specific Page in PDF in C++
If you want to insert an empty page into a specific position of a PDF document, you can use the PdfDocument->GetPages()->Insert(int index) method. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument->LoadFromFile() method.
- Create a blank page and insert it after a specified PDF page using PdfDocument->GetPages()->Insert(int index) method.
- Save the result document using PdfDocument->SaveToFile () method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input and output file paths
wstring inputFile = L"Data\\input.pdf";
wstring outputFile = L"Output\\InsertEmptyPage.pdf";
//Create a PdfDocument object
PdfDocument* pdf = new PdfDocument();
//Load a PDF document
pdf->LoadFromFile(inputFile.c_str());
//Insert a blank page as the second page
pdf->GetPages()->Insert(1);
//Save the result document
pdf->SaveToFile(outputFile.c_str());
pdf->Close();
delete pdf;
}

Delete an Existing Page in a PDF Document in C++
For PDF pages containing useless information, you can also remove them using Spire.PDF for C++. The following are the steps to delete a specific page in PDF.
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument->LoadFromFile() method.
- Remove a specified page from the document using PdfDocument->GetPages()->RemoveAt(int index) method.
- Save the result document using PdfDocument->SaveToFile () method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input and output file paths
wstring inputFile = L"Data\\input.pdf";
wstring outputFile = L"Output\\DeletePage.pdf";
//Create a PdfDocument object
PdfDocument* pdf = new PdfDocument();
//Load a PDF document
pdf->LoadFromFile(inputFile.c_str());
//Delete the first page in the document
pdf->GetPages()->RemoveAt(0);
//Save the result document
pdf->SaveToFile(outputFile.c_str());
pdf->Close();
delete pdf;
}

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.
Merging Excel files is an essential task when you need to summarize data stored in multiple Excel files. For instance, if you have sales reports for each quarter of the year, you might need to merge them into one file to get a more comprehensive view of the data for the entire year. By merging Excel files, you are able to concentrate on a single organized workbook instead of switching between multiple files. This streamlines your work process and improves efficiency. In this article, you will learn how to merge Excel files into one in C++ using Spire.XLS for C++ library.
Install Spire.XLS for C++
There are two ways to integrate Spire.XLS 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.XLS for C++ in a C++ Application
Merge Multiple Excel Workbooks into One in C++
You can merge multiple Excel workbooks into one by creating a new workbook, then copying worksheets in the original workbooks to the new workbook. The detailed steps are as follows:
- Put the paths of the original workbooks into a vector.
- Initialize a Workbook object to create a new workbook and clear the default worksheets in it.
- Initialize a temporary Workbook object.
- Iterate through the workbooks in the vector.
- Load the workbook into the temporary Workbook object using Workbook->LoadFromFile() method.
- Iterate through the worksheets in the workbook, then copy each worksheet from the workbook to the new workbook using Workbook->GetWorksheets()->AddCopy() method.
- Save the result workbook to file using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
int main()
{
//Put the paths of the workbooks into a vector
std::vector<std::wstring> files = { L"File1.xlsx", L"File2.xlsx", L"File3.xlsx" };;
//Initialize a Workbook object to create a new workbook
intrusive_ptr<Workbook> newWorkbook = new Workbook();
newWorkbook->SetVersion(ExcelVersion::Version2013);
//Clear the default worksheets
newWorkbook->GetWorksheets()->Clear();
//Initialize a temporary Workbook object
intrusive_ptr<Workbook> tempWorkbook = new Workbook();
//Iterate through the workbooks in the vector
for (auto file : files)
{
//Load the current workbook
tempWorkbook->LoadFromFile(file.c_str());
//Iterate through all worksheets in the workbook
for (int i = 0; i < tempWorkbook->GetWorksheets()->GetCount(); i++)
{
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(tempWorkbook->GetWorksheets()->Get(i));
//Copy each worksheet from the workbook to the new workbook
(intrusive_ptr<XlsWorksheetsCollection>(newWorkbook->GetWorksheets()))->AddCopy(sheet, WorksheetCopyType::CopyAll);
}
}
//Save the result workbook to file
newWorkbook->SaveToFile(L"MergeExcelFiles.xlsx", ExcelVersion::Version2013);
newWorkbook->Dispose();
tempWorkbook->Dispose();
}

Merge Multiple Excel Worksheets into One in C++
You can merge multiple worksheets into one worksheet by copying the used data range in the original worksheets to the destination worksheet. The following steps show you how to merge two worksheets within the same workbook into one worksheet:
- Initialize a Workbook object and load an Excel workbook using Workbook->LoadFromFile() method.
- Get the two worksheets that need to be merged using Workbook->GetWorksheets()->Get(int index) method (the sheet index here is zero-based).
- Get the used range of the second worksheet using Worksheet->GetAllocatedRange() method.
- Specify the destination range in the first worksheet using Worksheet->GetRange(int row, int column) method (the row and column indexes here are 1-based).
- Copy the used range of the second worksheet to the destination range in the first worksheet using CellRange->Copy(CellRange destRange) method.
- Remove the second worksheet from the workbook using XlsWorksheet->Remove() method.
- Save the result workbook to file using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
using namespace std;
int main()
{
//Initialize a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load an Excel workbook
workbook->LoadFromFile(L"Sample.xlsx");
//Get the first worksheet
intrusive_ptr<Worksheet> sheet1 = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//Get the second worksheet
intrusive_ptr<Worksheet> sheet2 = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(1));
//Get the used range in the second worksheet
intrusive_ptr<CellRange> sourceRange = dynamic_pointer_cast<CellRange>(sheet2->GetAllocatedRange());
//Specify the destination range in the first worksheet
intrusive_ptr<CellRange> destRange = dynamic_pointer_cast<CellRange>(sheet1->GetRange(sheet1->GetLastRow() + 1, 1));
//Copy the used range of the second worksheet to the destination range in the first worksheet
sourceRange->Copy(destRange);
//Remove the second worksheet
sheet2->Remove();
//Save the result workbook to file
workbook->SaveToFile(L"MergeExcelWorksheets.xlsx", ExcelVersion::Version2013);
workbook->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.
The find and replace feature in Microsoft Word is an essential tool when editing documents. It enables you to quickly find a specific word or phrase in a Word document and lets you replace all instances of it at once. This is especially helpful in situations where you need to update information or correct misspelled words in large Word documents. In this article, you will learn how to find and replace text in Word documents in C++ using Spire.Doc for C++.
- Find Text and Replace All Its Instances with New Text
- Find Text and Replace Its First Instance with New Text
- Find and Replace Text Using a Regular Expression
- Find and Replace Text with an Image
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
Find Text and Replace All Its Instances with New Text
You can find a text and replace all its instances with another text easily using the Document->Replace() method. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Find a specific text and replace all its instances with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"Input.docx");
//Find a specific text and replace all its instances with another text
document->Replace(L"Spire.Doc", L"Eiceblue", false, true);
//Save the result document
document->SaveToFile(L"ReplaceAllInstances.docx", FileFormat::Docx2013);
document->Close();
}

Find Text and Replace Its First Instance with New Text
Spire.Doc for C++ provides the Document->SetReplaceFirst() method which enables you to change the replacement mode from replacing all instances to replacing the first instance. The following steps explain how to find a text and replace its first instance:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Change the replacement mode to replace the first instance using Document->SetReplaceFirst(true) method.
- Replace the first instance of a text with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> document = new Document();
//Load a Word document
document->LoadFromFile(L"Input.docx");
//Change the replacement mode to replace the first match
document->SetReplaceFirst(true);
//Replace the first instance of a text with another text
document->Replace(L"Spire.Doc", L"Eiceblue", false, true);
//Save the result document
document->SaveToFile(L"ReplaceFirstInstance.docx", FileFormat::Docx2013);
document->Close();
}

Find and Replace Text Using a Regular Expression
You can replace a text matching a regular expression with new text using the Document->Replace() method and passing a Regex instance and the new text to the method as parameters. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Initialize an instance of the Regex class to create a regular expression.
- Find the text matching the regex and replace it with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> doc = new Document();
//Load a Word document
doc->LoadFromFile(L"Input1.docx");
//Create a regex to match the text that starts with #
intrusive_ptr<Regex> regex = new Regex(L"\\#\\w+\\b");
//Find the text matching the regex and replace it with another text
doc->Replace(regex, L"Monitor");
//Save the result document
doc->SaveToFile(L"ReplaceWithRegex.docx", FileFormat::Docx2013);
doc->Close();;
}

Find and Replace Text with an Image
Spire.Doc for C++ doesn't offer a direct method to replace text with image, but you can achieve this by inserting the image at the position of the text and then removing the text from the document. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Find a specific text using Document->FindAllString() method and put the found text into a vector.
- Iterate through the found text in the vector.
- Initialize an instance of DocPicture class and load an image using DocPicture->LoadImageSpire() method.
- Get the found text as a single text range and then get the index of the text range in its owner paragraph.
- Insert an image at the position of the text range and then remove the text range from the document.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
//Initialize an instance of the Document class
intrusive_ptr<Document> doc = new Document();
//Load a Word document
doc->LoadFromFile(L"Input.docx");
//Find a text in the document and put the found results into a vector
vector<intrusive_ptr<TextSelection>> selections = doc->FindAllString(L"Spire.Doc", true, true);
int index = 0;
intrusive_ptr<TextRange> range = nullptr;
//Iterate through the found text in the vector
for (auto selection : selections)
{
//Load an image
intrusive_ptr<DocPicture> pic = new DocPicture(doc);
pic->LoadImageSpire(L"img.jpg");
//Get the found text as a single text range
range = selection->GetAsOneRange();
//Get the index of the text range in its owner paragraph
index = range->GetOwnerParagraph()->GetChildObjects()->IndexOf(range);
//Insert an image at the index
range->GetOwnerParagraph()->GetChildObjects()->Insert(index, pic);
//Remove the text range
range->GetOwnerParagraph()->GetChildObjects()->Remove(range);
}
//Save the result document
doc->SaveToFile(L"ReplaceWithImage.docx", FileFormat::Docx2013);
doc->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.