Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.Chilkat Lazarus Pascal Downloads
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;
reader: TPdf;
info: 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 is available at encrypted_user_password.pdf
pdf := TPdf.Create;
// Removing the encryption needs owner rights: load with the owner password.
pdf.OwnerPassword := 'owner-secret';
success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.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('encrypted, as loaded: ' + jsonOut.BoolOf('loaded.encrypted'));
WriteLn('algorithm: ' + jsonOut.StringOf('loaded.algorithm'));
success := pdf.Protection('removeEncryption',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('will be saved encrypted: ' + jsonOut.BoolOf('pending.encrypted'));
success := pdf.WriteFile('qa_output/minutes_unprotected.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// The result is an ordinary PDF that opens without a password.
reader := TPdf.Create;
info := TJsonObject.Create;
success := reader.PreLoadCheck('qa_output/minutes_unprotected.pdf',info);
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
WriteLn('encrypted: ' + info.BoolOf('encrypted'));
success := reader.LoadFile('qa_output/minutes_unprotected.pdf');
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
WriteLn('pages: ' + reader.NumPages);
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
pdf.Free;
json.Free;
jsonOut.Free;
reader.Free;
info.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.