How to Merge Multiple DOCX Documents using C/C++
2 min readAug 7, 2023
Merging relevant DOCX documents is both important and extremely tedious work. Thankfully, you can use the below code to accomplish that task programmatically and reinvest your time capital elsewhere. You can build this free API service into any of your C/C++ file processing applications and rapidly combine 10+ DOCX files via file paths in a single request.
Start by installing libcurl in your C/C++ project:
libcurl/7.75.0
After that, use the below examples to structure your request:
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/docx/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);
That’s all the code you’ll need. You can authorize up to 800 API calls per month free with a free-tier API key.