C Requires Chilkat v11.7.0+
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 C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject jsonOut;
HCkPdf reader;
HCkJsonObject 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 = CkPdf_Create();
// Removing the encryption needs owner rights: load with the owner password.
CkPdf_putOwnerPassword(pdf,"owner-secret");
success = CkPdf_LoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
return;
}
json = CkJsonObject_Create();
jsonOut = CkJsonObject_Create();
success = CkPdf_Protection(pdf,"getInfo",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("encrypted, as loaded: %d\n",CkJsonObject_BoolOf(jsonOut,"loaded.encrypted"));
printf("algorithm: %s\n",CkJsonObject_stringOf(jsonOut,"loaded.algorithm"));
success = CkPdf_Protection(pdf,"removeEncryption",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("will be saved encrypted: %d\n",CkJsonObject_BoolOf(jsonOut,"pending.encrypted"));
success = CkPdf_WriteFile(pdf,"qa_output/minutes_unprotected.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// The result is an ordinary PDF that opens without a password.
reader = CkPdf_Create();
info = CkJsonObject_Create();
success = CkPdf_PreLoadCheck(reader,"qa_output/minutes_unprotected.pdf",info);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(reader));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(reader);
CkJsonObject_Dispose(info);
return;
}
printf("encrypted: %d\n",CkJsonObject_BoolOf(info,"encrypted"));
success = CkPdf_LoadFile(reader,"qa_output/minutes_unprotected.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(reader));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(reader);
CkJsonObject_Dispose(info);
return;
}
printf("pages: %d\n",CkPdf_getNumPages(reader));
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(reader);
CkJsonObject_Dispose(info);
}