C++: Merge Multiple PDF Files into a Single PDF

Merging multiple PDF files into a single PDF can help you reduce clutter and let you read, print, and share the files more easily. After merging, you only need to deal with one file instead of multiple files. In this article, you will learn how to merge multiple PDF files into a single PDF in C++ using Spire.PDF for 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

Merge Multiple PDF Files into a Single PDF in C++

Spire.PDF for C++ offers a static method - PdfDocument::MergeFiles(std::vector<LPCWSTR_S> inputFiles) which enables you to merge multiple PDF files into a single PDF file easily. The following are the detailed steps:

  • Put the input PDF files' paths into a vector.
  • Merge the PDF files specified by the paths in the vector using PdfDocument::MergeFiles(std::vector<LPCWSTR_S> inputFiles) method.
  • Specify the output file path.
  • Save the result PDF file using PdfDocumentBase->Save() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;
using namespace Spire::Common;

int main() {
	//Put the input PDF files' paths into a vector
	std::vector<LPCWSTR_S> files = { L"Input\\File_1.pdf", L"Input\\File_2.pdf", L"Input\\File_3.pdf" };

	//Merge the PDF files specified by the paths in the vector
	PdfDocumentBase* doc = PdfDocument::MergeFiles(files);

	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result PDF file
	doc->Save(outputFile.c_str(), FileFormat::PDF);
	doc->Close();
}

C++: Merge Multiple PDF Files into a Single PDF

Merge Multiple PDF Files from Streams in C++

You can use the PdfDocument::MergeFiles(std::vector< Stream*> streams) method to merge multiple PDF streams into a single PDF. The following are the detailed steps:

  • Read the input PDF files into streams.
  • Put the streams into a vector.
  • Merge the PDF streams using PdfDocument::MergeFiles(std::vector< Stream*> streams) method.
  • Save the result PDF file using PdfDocumentBase->Save() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;
using namespace Spire::Common;

int main() {	
	//Read the input PDF files into streams
	Stream* stream1 = new Stream(L"Input\\File_1.pdf");
	Stream* stream2 = new Stream(L"Input\\File_2.pdf");
	Stream* stream3 = new Stream(L"Input\\File_3.pdf");

	//Put the streams into a vector
	std::vector<Stream*> streams = { stream1, stream2, stream3 };

	//Merge the PDF streams
	PdfDocumentBase* doc = PdfDocument::MergeFiles(streams);

	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result PDF file
	doc->Save(outputFile.c_str(), FileFormat::PDF);
	doc->Close();
}

Merge Selected Pages of PDF Files into a Single PDF in C++

You can merge a specific page or a range of pages of multiple PDF files into a single PDF file using PdfDocument->InsertPage(PdfDocument ldDoc, int pageIndex) or PdfDocument->InsertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method. The following are the detailed steps:

  • Put the input PDF files' paths into a vector.
  • Create a vector of PdfDocument objects.
  • Iterate through the paths in the vector.
  • Load the PDF files specified by the paths using PdfDocument->LoadFromFile() method.
  • Initialize an instance of PdfDocument class to create a new PDF document.
  • Insert a specific page or a range of pages from the loaded PDF files into the new PDF using PdfDocument->InsertPage(PdfDocument ldDoc, int pageIndex) or PdfDocument->InsertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method.
  • Save the result PDF using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;
using namespace Spire::Common;

int main() {
	
	//Put the input PDF files' paths into a vector
	std::vector<std::wstring> files = { L"Input\\File_2.pdf", L"Input\\File_4.pdf" };
	
	//Create a vector of PdfDocument objects           
	std::vector<PdfDocument*> docs(files.size());
	
	//Iterate through the paths in the vector
	for (int i = 0; i < files.size(); i++)
	{
		//Load the PDF files specified by the paths
		docs[i] = new PdfDocument();
		docs[i]->LoadFromFile(files[i].c_str());
	}

	//Create a new PDF document
	PdfDocument* newDoc = new PdfDocument();

	//Insert pages 1-2 of the first PDF into the new PDF
	newDoc->InsertPageRange(docs[0], 0, 1);
	//Insert page 1 of the second PDF into the new PDF
	newDoc->InsertPage(docs[1], 0);

	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result pdf file
	newDoc->SaveToFile(outputFile.c_str());

	//Close the PdfDocument objects
	newDoc->Close();
	for (auto doc : docs)
	{
		doc->Close();
	}
}

C++: Merge Multiple PDF Files into a Single 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.