How to Convert DOCX to PDF in C/C++

Cloudmersive
2 min readMay 14, 2021

With its consistent reputation as the most common and widely used of the Microsoft Office applications, Microsoft Word documents are employed by students, professionals, and hobbyists alike. However, while DOCX files are great for ease of use and design, they do provide some limitations when it comes to compatibility. One way to solve the compatibility issue is to convert the document to the display-friendly PDF format, and while this may be a simple process for a handful of documents, it becomes more difficult if you have a large amount of documents that need to be converted. The following API can be used in C/C++ to simplify the conversion and save you a whole lot of time.

First, we will need to install libcurl:

libcurl/7.75.0

Next, input the file you wish to perform the operation on, along with your API key, into 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/docx/to/pdf");
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, "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);

And that’s it! Easy.

--

--

Cloudmersive

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