How to Merge PDF Documents Together using C/C++
Multi-page PDF documents can house a wide variety of content in a single file, making it easy to assemble & share large projects. Using the below code, you can make that process even easier — you can take advantage of a free API that programmatically combines PDF files into a single document using form data.
To call this API, start by installing Libcurl in your C/C++ project:
libcurl/7.75.0
Then copy & paste the below code examples into your file and provide a free-tier API key (this allows 800 API calls per month with no additional commitment) to authorize your requests:
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);
You can combine 10+ PDF files in your request; the resulting document will display the files in the order they were entered. That’s all there is to it!