Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.Chilkat Unicode C++ Downloads
#include <CkPdfW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at encrypted_user_password.pdf
CkPdfW pdf;
// Removing the encryption needs owner rights: load with the owner password.
pdf.put_OwnerPassword(L"owner-secret");
success = pdf.LoadFile(L"qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
CkJsonObjectW json;
CkJsonObjectW jsonOut;
success = pdf.Protection(L"getInfo",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"encrypted, as loaded: %d\n",jsonOut.BoolOf(L"loaded.encrypted"));
wprintf(L"algorithm: %s\n",jsonOut.stringOf(L"loaded.algorithm"));
success = pdf.Protection(L"removeEncryption",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"will be saved encrypted: %d\n",jsonOut.BoolOf(L"pending.encrypted"));
success = pdf.WriteFile(L"qa_output/minutes_unprotected.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// The result is an ordinary PDF that opens without a password.
CkPdfW reader;
CkJsonObjectW info;
success = reader.PreLoadCheck(L"qa_output/minutes_unprotected.pdf",info);
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
wprintf(L"encrypted: %d\n",info.BoolOf(L"encrypted"));
success = reader.LoadFile(L"qa_output/minutes_unprotected.pdf");
if (success == false) {
wprintf(L"%s\n",reader.lastErrorText());
return;
}
wprintf(L"pages: %d\n",reader.get_NumPages());
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
}