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 <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
HCkPdfW reader;
HCkJsonObjectW info;
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
pdf = CkPdfW_Create();
// Removing the encryption needs owner rights: load with the owner password.
CkPdfW_putOwnerPassword(pdf,L"owner-secret");
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/encrypted_user_password.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_Protection(pdf,L"getInfo",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"encrypted, as loaded: %d\n",CkJsonObjectW_BoolOf(jsonOut,L"loaded.encrypted"));
wprintf(L"algorithm: %s\n",CkJsonObjectW_stringOf(jsonOut,L"loaded.algorithm"));
success = CkPdfW_Protection(pdf,L"removeEncryption",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"will be saved encrypted: %d\n",CkJsonObjectW_BoolOf(jsonOut,L"pending.encrypted"));
success = CkPdfW_WriteFile(pdf,L"qa_output/minutes_unprotected.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// The result is an ordinary PDF that opens without a password.
reader = CkPdfW_Create();
info = CkJsonObjectW_Create();
success = CkPdfW_PreLoadCheck(reader,L"qa_output/minutes_unprotected.pdf",info);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
return;
}
wprintf(L"encrypted: %d\n",CkJsonObjectW_BoolOf(info,L"encrypted"));
success = CkPdfW_LoadFile(reader,L"qa_output/minutes_unprotected.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
return;
}
wprintf(L"pages: %d\n",CkPdfW_getNumPages(reader));
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(reader);
CkJsonObjectW_Dispose(info);
}