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

Optimizing an Encrypted PDF

See more PDF Optimization Examples

Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
noPassword: HCkPdf;
q: HCkJsonObject;
qOut: HCkJsonObject;
withPassword: HCkPdf;

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();

//  The password must be set before loading.
CkPdf_putUserPassword(pdf,'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;

//  What security does the file have, and may it be modified?
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_Protection(pdf,'getInfo',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('encrypted: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'loaded.encrypted'))));
Memo1.Lines.Add('method:    ' + CkJsonObject__stringOf(jsonOut,'loaded.method'));
Memo1.Lines.Add('openedWith: ' + CkJsonObject__stringOf(jsonOut,'loaded.openedWith'));
Memo1.Lines.Add('may modify: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'loaded.permissions.modify'))));

success := CkPdf_Optimize(pdf,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
success := CkPdf_WriteFile(pdf,'qa_output/encrypted_optimized.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('bytesSaved: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'bytesSaved')));

//  The output is still encrypted with the same security: loading it without the password fails.
noPassword := CkPdf_Create();
success := CkPdf_LoadFile(noPassword,'qa_output/encrypted_optimized.pdf');
q := CkJsonObject_Create();
qOut := CkJsonObject_Create();
success := CkPdf_ExtractText(noPassword,'assess',q,qOut);
Memo1.Lines.Add('passwordRequired without the password: ' + IntToStr(Ord(CkJsonObject_BoolOf(qOut,'quality.passwordRequired'))));

//  With the password it reads normally.
withPassword := CkPdf_Create();
CkPdf_putUserPassword(withPassword,'secret');
success := CkPdf_LoadFile(withPassword,'qa_output/encrypted_optimized.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(withPassword));
    Exit;
  end;
Memo1.Lines.Add('pages: ' + IntToStr(CkPdf_getNumPages(withPassword)));

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(noPassword);
CkJsonObject_Dispose(q);
CkJsonObject_Dispose(qOut);
CkPdf_Dispose(withPassword);