Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

Extract Text from a Password-Protected PDF

See more PDF Text Extraction Examples

Demonstrates extracting text from a PDF that requires a password to open. The PDF loads without the password (its structure can be read), but the "text" operation fails because the content cannot be decrypted; "assess" reports that a password is required. With the UserPassword property set before loading, extraction works normally.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Pdf,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;

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 (user password: secret) is available at encrypted_user_password.pdf

  pdf := TPdf.Create;
  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;

  //  The PDF loads without its password: the file's structure is readable, so properties such as
  //  NumPages and the security settings are available...
  success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  ...but the content cannot be decrypted, so extracting text fails.
  success := pdf.ExtractText('text',json,jsonOut);
  if (success = False) then
    begin
      WriteLn('Cannot extract: ' + pdf.LastErrorText);
    end;

  //  "assess" still works: it reports that a password is required and nothing else.
  success := pdf.ExtractText('assess',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('passwordRequired: ' + jsonOut.BoolOf('quality.passwordRequired'));
  WriteLn('rating: ' + jsonOut.StringOf('quality.rating'));

  //  Set the password and load again.
  pdf.UserPassword := 'secret';
  success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  success := pdf.ExtractText('text',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn(jsonOut.StringOf('text'));

  //  Sample output:

  //  Cannot extract: ChilkatLog:
  //    ExtractText:
  //      ...
  //        The PDF is encrypted and could not be opened (wrong or missing password); its text cannot be read.
  //      ...
  //  passwordRequired: true
  //  rating: none
  //  Board Minutes (Confidential)
  //  
  //  The board met on 3 November 2026. Present: the chair, the treasurer and four members. The treasurer reported that spending for the season came in under budget.
  //  
  //  The meeting closed at 8.45 p.m.


  pdf.Free;
  json.Free;
  jsonOut.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.