How to Merge PDF Files Together in C/C++

Cloudmersive
2 min readApr 9, 2021

In previous articles we have discussed the fact that PDFs are the ideal format to use when sharing information with internal or external partners. However, if you need to share several PDF documents at the same time, it can be beneficial to merge similar documents into one file. This is particularly true if you are sharing or collecting order forms, contracts, or invoices; storing related documents in one file will significantly improve your organization, especially if the forms need to be sorted in a specific way. The following API will allow you to merge multiple PDF files into a single document that can then be stored or shared as needed.

To use the API in C/C++, we first need to install libcurl into the project:

libcurl/7.75.0

Now we’re ready to call the merge function with the following code:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/merge/pdf/multi");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile1");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile2");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile3");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile4");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile5");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile6");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile7");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile8");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile9");
curl_mime_filedata(part, "/path/to/file");
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile10");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);

And we’re done! Your merged document will be ready for download and use immediately.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.