How to Remove (Delete) Specific Pages from a PDF Document using C/C++
When it comes time to edit PDF files at scale, there’s a lot we can accomplish without ever opening an actual document on our device. Using the ready-to-run C/C++ code provided below, we can leverage an API that removes pages from a PDF file based on a page range (pageStart to pageEnd), accepting multipart/form-data input.
To take advantage of this solution, we’ll first need to install Libcurl in our project:
libcurl/7.75.0
After that, we can structure your API call using the below examples, and we can authorize our request with a free-tier API key (good for 800 API calls per month with no commitment):
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/pages/delete");
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, "pageStart: <integer>");
headers = curl_slist_append(headers, "pageEnd: <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);
At this point we’re good to go — no more code required. It’s just that simple!