Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Change the Permissions of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
reader: HCkPdf;
empty: HCkJsonObject;
info: HCkJsonObject;
begin
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();
// Changing the permissions needs owner rights. Both passwords are given here, which is what
// an AES-256 PDF requires for this operation.
CkPdf_putUserPassword(pdf,'secret');
CkPdf_putOwnerPassword(pdf,'owner-secret');
success := CkPdf_LoadFile(pdf,'qa_data/pdf/encrypted_user_password.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
// No printing and no copying. Everything not mentioned is granted.
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'permissions.print','none');
CkJsonObject_UpdateBool(json,'permissions.copy',False);
jsonOut := CkJsonObject_Create();
success := CkPdf_Protection(pdf,'changePermissions',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('print: ' + CkJsonObject__stringOf(jsonOut,'pending.permissions.print'));
Memo1.Lines.Add('copy: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'pending.permissions.copy'))));
Memo1.Lines.Add('modify: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'pending.permissions.modify'))));
Memo1.Lines.Add('password still needed to open: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'pending.userPasswordRequired'))));
success := CkPdf_WriteFile(pdf,'qa_output/minutes_no_print.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
// The same user password opens the result, now with the new permissions.
reader := CkPdf_Create();
CkPdf_putUserPassword(reader,'secret');
success := CkPdf_LoadFile(reader,'qa_output/minutes_no_print.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(reader));
Exit;
end;
empty := CkJsonObject_Create();
info := CkJsonObject_Create();
success := CkPdf_Protection(reader,'getInfo',empty,info);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(reader));
Exit;
end;
Memo1.Lines.Add('saved file, print: ' + CkJsonObject__stringOf(info,'loaded.permissions.print'));
Memo1.Lines.Add('saved file, copy: ' + IntToStr(Ord(CkJsonObject_BoolOf(info,'loaded.permissions.copy'))));
// Sample output:
// print: none
// copy: False
// modify: True
// password still needed to open: True
// saved file, print: none
// saved file, copy: False
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(reader);
CkJsonObject_Dispose(empty);
CkJsonObject_Dispose(info);