Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
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 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;
noPassword: TPdf;
q: TJsonObject;
qOut: TJsonObject;
withPassword: TPdf;
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;
// The password must be set before loading.
pdf.UserPassword := 'secret';
success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// What security does the file have, and may it be modified?
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
success := pdf.Protection('getInfo',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('encrypted: ' + jsonOut.BoolOf('loaded.encrypted'));
WriteLn('method: ' + jsonOut.StringOf('loaded.method'));
WriteLn('openedWith: ' + jsonOut.StringOf('loaded.openedWith'));
WriteLn('may modify: ' + jsonOut.BoolOf('loaded.permissions.modify'));
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/encrypted_optimized.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('bytesSaved: ' + jsonOut.IntOf('bytesSaved'));
// The output is still encrypted with the same security: loading it without the password fails.
noPassword := TPdf.Create;
success := noPassword.LoadFile('qa_output/encrypted_optimized.pdf');
q := TJsonObject.Create;
qOut := TJsonObject.Create;
success := noPassword.ExtractText('assess',q,qOut);
WriteLn('passwordRequired without the password: ' + qOut.BoolOf('quality.passwordRequired'));
// With the password it reads normally.
withPassword := TPdf.Create;
withPassword.UserPassword := 'secret';
success := withPassword.LoadFile('qa_output/encrypted_optimized.pdf');
if (success = False) then
begin
WriteLn(withPassword.LastErrorText);
Exit;
end;
WriteLn('pages: ' + withPassword.NumPages);
pdf.Free;
json.Free;
jsonOut.Free;
noPassword.Free;
q.Free;
qOut.Free;
withPassword.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.