How to Reduce PDF File Size in C/C++

Cloudmersive
1 min readApr 5, 2021

--

While large PDF files are an inevitability for certain types of business documents, processing and loading times between programs and computers can run rather long. Utilizing the following API in C/C++ will allow you to reduce the file size of a PDF by optimizing its content.

To use the API, we first need to install libcurl into the project:

libcurl/7.75.0

Next, we simply need to input the target file and the API key into the example code below:

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/pdf/optimize/reduce-file-size");
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);

At the end of this quick process, you’ll receive a condensed PDF file for download. Visit the Cloudmersive website to retrieve your free API key and gain access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive

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