The Delphi example uses CreateOleObject, which keeps the project free of an imported type library. The same methods are available through an imported ActiveX wrapper if you prefer early binding.
It shows the complete round trip: load, inspect, encrypt, reload and decrypt.
Before you start
- Install the PDF Encrypt ActiveX DLL trial.
- Add the sample to a form with a button and an OpenDialog.
- Set your license key before the first operation.
- Check each return code and close the document before unloading the object.
Complete example
Delphi
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
obj: OleVariant;
iRtn, iPermissions: Integer;
sInFile, sOutFile: string;
begin
OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName);
if not OpenDialog1.Execute then
begin
Exit;
end else
begin
sInFile := OpenDialog1.FileName;
sOutFile := ChangeFileExt(sInFile, '') + '_CanNot_Print_Copy_Edit.pdf';
end;
try
obj := CreateOleObject('PDFEncryptX.EncryptLib'); //Create object
except
on E: Exception do
begin
ShowMessage('Please install AzSDK PDF Encrypt ActiveX DLL first.');
Exit;
end;
end;
obj.SetLicenseKey('License Code'); //Set your license key
iRtn := obj.LoadFromFile(sInFile, ''); //Load PDF file
{
iRtn = 1: loaded successful
iRtn = 0: Can not Load
iRtn =-1: File not exist
}
//if load file successful, then get relational property.
if iRtn = 1 then
begin
if obj.IsEncrypted = 1 then //Encrypted PDF ?
edtEncrypted.Text := '1->YES'
else edtEncrypted.Text := '0->NO';
edtPage.Text := IntToStr(obj.PageCount); //Page count of PDF
edtSize.Text := IntToStr(obj.FileSize); //File size of PDF
end;
//Create Permissions: Can not to print, copy and edit.
iPermissions := obj.CreatePermissions(0, 0, 0, 1, 1, 1, 1, 1);
//Call Encrypt method
iRtn := obj.Encrypt(sOutFile, 2, iPermissions, '', '');
case iRtn of
0: edtResult.Text := '0->Encrypt failed';
1: edtResult.Text := '1->Encrypt successful';
2: edtResult.Text := '2->Ignore - file already encrypted';
end;
Obj := Unassigned;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
end.How it works
- Wrap
CreateOleObjectin a try/except block so a missing component produces a clear message. - Use
Obj := Unassignedto release the object. - The output file name is derived from the input file in the sample; change it to match your workflow.
During evaluation, leave the placeholder license key in place. After purchase, replace it with the key from your email.