How to Convert a Word DOCX File to JPG in C/C++

Due to its internal design, manually converting a DOCX file to another format is never easy. And if you have a multitude of files to perform the conversion, the difficulty is amplified. In this quick tutorial, we will provide instructions on how you can use an API in C/C++ to cut down processing time and automatically convert a Word document to a JPG/JPEG image array (one for each page).

Let’s begin by installing libcurl into our C/C++ project:

libcurl/7.75.0

Then, we can input our file and call the function:

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/docx/to/jpg");
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, "quality: <integer>");
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, "inputFile");
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);

There is also the option to customize the image quality level between 1–100, but the default level is 75. If you have questions on this API or need to obtain your free API key, simply head over to the Cloudmersive website.