Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.7.0+

Change the Passwords of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
pdf: TChilkatPdf;
json: TChilkatJsonObject;
jsonOut: TChilkatJsonObject;
reader: TChilkatPdf;
owner: TChilkatPdf;

begin
success := 0;

//  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 := TChilkatPdf.Create(Self);

//  Load with the current passwords.
pdf.UserPassword := 'secret';
pdf.OwnerPassword := 'owner-secret';
success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
if (success = 0) then
  begin
    Memo1.Lines.Add(pdf.LastErrorText);
    Exit;
  end;

//  The new passwords.  Protection reads the properties as they are when it is called.
pdf.UserPassword := 'new-user-secret';
pdf.OwnerPassword := 'new-owner-secret';

json := TChilkatJsonObject.Create(Self);
jsonOut := TChilkatJsonObject.Create(Self);
success := pdf.Protection('changePasswords',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(pdf.LastErrorText);
    Exit;
  end;

//  The algorithm and the permissions are kept.
Memo1.Lines.Add('algorithm: ' + jsonOut.StringOf('pending.algorithm'));
Memo1.Lines.Add('password needed to open: ' + IntToStr(Ord(jsonOut.BoolOf('pending.userPasswordRequired'))));

success := pdf.WriteFile('qa_output/minutes_new_passwords.pdf');
if (success = 0) then
  begin
    Memo1.Lines.Add(pdf.LastErrorText);
    Exit;
  end;

//  The new user password opens the result.
reader := TChilkatPdf.Create(Self);
reader.UserPassword := 'new-user-secret';
success := reader.LoadFile('qa_output/minutes_new_passwords.pdf');
if (success = 0) then
  begin
    Memo1.Lines.Add(reader.LastErrorText);
    Exit;
  end;
success := reader.Protection('getInfo',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(reader.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('with the new user password, opened with: ' + jsonOut.StringOf('loaded.openedWith'));

//  And so does the new owner password.
owner := TChilkatPdf.Create(Self);
owner.OwnerPassword := 'new-owner-secret';
success := owner.LoadFile('qa_output/minutes_new_passwords.pdf');
if (success = 0) then
  begin
    Memo1.Lines.Add(owner.LastErrorText);
    Exit;
  end;
success := owner.Protection('getInfo',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(owner.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('with the new owner password, opened with: ' + jsonOut.StringOf('loaded.openedWith'));

//  Sample output:

//  algorithm: rc4-128
//  password needed to open: True
//  with the new user password, opened with: user
//  with the new owner password, opened with: owner