How to Get Images from a DOCX Document using C/C++

Cloudmersive
2 min readAug 15, 2023

--

The efficient organization of OpenXML format documents makes it extremely easy to programmatically retrieve data from within a document’s file structure and return that data independently of other entities.

Using the below code, you can call a free API which specifically retrieves image files stored within a DOCX file. Your request will return a URL containing the image contents, along with a wide range of additional metadata including the image name, image ID, image description (if available), and details regarding the orientation of that image within the original document.

To structure your API call, first install libcurl in your C/C++ project:

libcurl/7.75.0

Then copy the below code into your file, and supply a free-tier Cloudmersive API key to authenticate your request (you can get one by registering a free account on the Cloudmersive website):

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/edit/docx/get-images");
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: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "InputFileBytes=%3Cbyte%3E&InputFileUrl=%3Cstring%3E";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

That’s all there is to it — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet