Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.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.StringBuilder,
Chilkat.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
reader: TPdf;
empty: TJsonObject;
info: TJsonObject;
opts: TJsonObject;
extractOut: TJsonObject;
sb: TStringBuilder;
owner: TPdf;
ownerInfo: 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 two_page_memo.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/two_page_memo.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// No user password: the PDF opens without a prompt. The owner password is what protects the
// restrictions, so it must not be empty when anything is restricted.
pdf.UserPassword := '';
pdf.OwnerPassword := 'owner-secret';
json := TJsonObject.Create;
json.UpdateString('permissions.print','lowQuality');
json.UpdateBool('permissions.copy',False);
json.UpdateBool('permissions.modify',False);
json.UpdateBool('permissions.annotate',False);
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
jsonOut := TJsonObject.Create;
success := pdf.Protection('encrypt',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/memo_restricted.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// Anyone can open the result, with the rights of a user.
reader := TPdf.Create;
success := reader.LoadFile('qa_output/memo_restricted.pdf');
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
empty := TJsonObject.Create;
info := TJsonObject.Create;
success := reader.Protection('getInfo',empty,info);
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
WriteLn('opened with: ' + info.StringOf('loaded.openedWith'));
WriteLn('print: ' + info.StringOf('loaded.permissions.print'));
WriteLn('copy: ' + info.BoolOf('loaded.permissions.copy'));
WriteLn('modify: ' + info.BoolOf('loaded.permissions.modify'));
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
opts := TJsonObject.Create;
extractOut := TJsonObject.Create;
sb := TStringBuilder.Create;
success := reader.ExtractTextSb('text',opts,sb,extractOut);
if (success = False) then
begin
WriteLn('Text extraction was refused: the PDF does not permit copying.');
end;
// The owner password lifts every restriction.
owner := TPdf.Create;
owner.OwnerPassword := 'owner-secret';
success := owner.LoadFile('qa_output/memo_restricted.pdf');
if (success = False) then
begin
WriteLn(owner.LastErrorText);
Exit;
end;
ownerInfo := TJsonObject.Create;
success := owner.Protection('getInfo',empty,ownerInfo);
if (success = False) then
begin
WriteLn(owner.LastErrorText);
Exit;
end;
WriteLn('opened with: ' + ownerInfo.StringOf('loaded.openedWith'));
success := owner.ExtractTextSb('text',opts,sb,extractOut);
if (success = False) then
begin
WriteLn(owner.LastErrorText);
Exit;
end;
WriteLn('characters extracted with the owner password: ' + sb.Length);
// Sample output:
// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048
pdf.Free;
json.Free;
jsonOut.Free;
reader.Free;
empty.Free;
info.Free;
opts.Free;
extractOut.Free;
sb.Free;
owner.Free;
ownerInfo.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.