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

Remove the Restrictions from a PDF

See more PDF Protection Examples

Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.

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.StringBuilder,
  Chilkat.Pdf,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  reader: TPdf;
  opts: TJsonObject;
  extractOut: TJsonObject;
  sb: TStringBuilder;

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 no_copy_permission.pdf

  pdf := TPdf.Create;

  //  This PDF opens without a password, but copying text from it is not permitted.
  //  Removing the restrictions needs owner rights: load with the owner password.
  pdf.OwnerPassword := 'owner-secret';
  success := pdf.LoadFile('qa_data/pdf/no_copy_permission.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  success := pdf.Protection('getInfo',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('may copy, before: ' + jsonOut.BoolOf('loaded.permissions.copy'));
  WriteLn('password needed to open: ' + jsonOut.BoolOf('loaded.userPasswordRequired'));

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

  //  With no user password and nothing restricted there is nothing left to protect, so the PDF
  //  will be saved unencrypted.  A PDF that needs a password to open would stay encrypted and
  //  keep its password.
  WriteLn('will be saved encrypted: ' + jsonOut.BoolOf('pending.encrypted'));

  success := pdf.WriteFile('qa_output/leaflet_unrestricted.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  Anyone can now extract the text.
  reader := TPdf.Create;
  success := reader.LoadFile('qa_output/leaflet_unrestricted.pdf');
  if (success = False) then
    begin
      WriteLn(reader.LastErrorText);
      Exit;
    end;
  opts := TJsonObject.Create;
  extractOut := TJsonObject.Create;
  sb := TStringBuilder.Create;
  success := reader.ExtractTextSb('text',opts,sb,extractOut);
  if (success = False) then
    begin
      WriteLn(reader.LastErrorText);
      Exit;
    end;
  WriteLn('characters extracted: ' + sb.Length);

  //  Sample output:

  //  may copy, before: False
  //  password needed to open: False
  //  will be saved encrypted: False
  //  characters extracted: 216


  pdf.Free;
  json.Free;
  jsonOut.Free;
  reader.Free;
  opts.Free;
  extractOut.Free;
  sb.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.