How to Decrypt and Remove Password Protection from a PDF using C/C++
When our applications process PDF files with encryption and password-protection measures in place, it certainly helps to have an efficient means for removing encryption and unlocking the file programmatically.
You can use the code provided below for exactly that purpose. By structuring an API call with the C/C++ examples below, you’ll be able to easily decrypt PDF files and enter a password to unlock them through simple form-data & text input fields.
First, install libcurl in your project:
libcurl/7.75.0
Then use the below code to structure your request, and supply a free-tier Cloudmersive API key to authenticate:
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/decrypt");
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, "password: <string>");
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);
Yep — that’s all the code you’ll need. With a free-tier API key, you’ll be able to perform this operation up to 800 times per month, and once you reach that limit, your total will reset the following month. Easy as can be!