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

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
checker: HCkPdf;
info: HCkJsonObject;
reader: 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 two_page_memo.pdf

pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/two_page_memo.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  The passwords are taken from these two properties at the time Protection is called.
//  The user password opens the PDF.  The owner password opens it too, and is the one needed
//  later to change or remove the protection.
CkPdf_putUserPassword(pdf,'user-secret');
CkPdf_putOwnerPassword(pdf,'owner-secret');

//  An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_Protection(pdf,'encrypt',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
Memo1.Lines.Add('algorithm: ' + CkJsonObject__stringOf(jsonOut,'pending.algorithm'));
Memo1.Lines.Add('PDF version: ' + CkJsonObject__stringOf(jsonOut,'pending.pdfVersion'));
Memo1.Lines.Add('password needed to open: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'pending.userPasswordRequired'))));

//  Writing the file applies it.
success := CkPdf_WriteFile(pdf,'qa_output/memo_encrypted.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  PreLoadCheck reports a file's encryption without loading it, and needs no password.
checker := CkPdf_Create();
info := CkJsonObject_Create();
success := CkPdf_PreLoadCheck(checker,'qa_output/memo_encrypted.pdf',info);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(checker));
    Exit;
  end;
Memo1.Lines.Add('encrypted: ' + IntToStr(Ord(CkJsonObject_BoolOf(info,'encrypted'))));
Memo1.Lines.Add('openPasswordRequired: ' + IntToStr(Ord(CkJsonObject_BoolOf(info,'openPasswordRequired'))));

//  With the password, the PDF opens normally.
reader := CkPdf_Create();
CkPdf_putUserPassword(reader,'user-secret');
success := CkPdf_LoadFile(reader,'qa_output/memo_encrypted.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(reader));
    Exit;
  end;
Memo1.Lines.Add('pages: ' + IntToStr(CkPdf_getNumPages(reader)));

//  Sample output:

//  algorithm: aes-256
//  PDF version: 1.7
//  password needed to open: True
//  encrypted: True
//  openPasswordRequired: True
//  pages: 2

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(checker);
CkJsonObject_Dispose(info);
CkPdf_Dispose(reader);