This Visual Basic 6 example uses late binding, so the project compiles even before the component is referenced. At runtime, the PDF Encrypt ActiveX DLL must be installed and registered on the machine.
The sample loads a PDF, reports whether it is already encrypted, applies a permission mask and saves a protected copy.
Before you start
- Install the trial build on the development machine.
- Paste the sample into a form with a command button.
- Call
SetLicenseKey()with your key, or use the placeholder during evaluation. - Check the return values after
Encrypt()and handle the already-encrypted case.
Complete example
Private Sub Command1_Click()
Dim obj As Object
Dim iRtn As Long
Dim iPermissions As Long
Set obj = CreateObject("PDFEncryptX.EncryptLib") 'Create object
obj.SetLicenseKey ("License Code") 'Set your license key
iRtn = obj.LoadFromFile(".\PDFEncryptX.pdf", "") 'Load PDF file
'iRtn = 1: loaded successful
'iRtn = 0: Load Failed;
'iRtn = -1: File not exist
'if load file successful, then get relational property.
If iRtn = 1 Then
If obj.IsEncrypted = 1 Then 'Encrypted PDF ?
Text1.Text = "YES"
Else
Text1.Text = "NO"
End If
Text2.Text = obj.PageCount 'Page count of PDF
Text3.Text = obj.FileSize 'File size of PDF
End If
'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(".\PDFEncryptX_CanNot_Print_Copy_Edit.pdf", 2, iPermissions, "", "")
If iRtn = 0 Then
Text4.Text = "0->Encrypt failed"
ElseIf iRtn = 1 Then
Text4.Text = "1->Encrypt successful"
ElseIf iRtn = 2 Then
Text4.Text = "2->Ignore - file already encrypted"
End If
Set obj = Nothing
End SubHow it works
- Late binding (
CreateObject) avoids a reference to the type library. - Use
Set obj = Nothingto release the COM object when finished. - For 64-bit processes, install and register the 64-bit build.