''' <summary>
''' Performs the authentication using the specified password.
''' </summary>
Public Shared Sub Authenticate(document As Vintasoft.Imaging.Pdf.PdfDocument, password As String)
If document.AuthorizationResult = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
document.Authenticate(password)
If document.AuthorizationResult = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
Throw New System.ArgumentException("Password is incorrect.")
End If
End If
End Sub
''' <summary>
''' Decrypts the specified document.
''' </summary>
Public Shared Sub Decrypt(document As Vintasoft.Imaging.Pdf.PdfDocument, password As String)
' authentication
Authenticate(document, password)
' pack document without encryption
document.Pack(document.Format, Nothing)
End Sub
''' <summary>
''' Encrypts PDF document with specified user password, owner password and
''' user access permissions.
''' </summary>
Public Shared Sub Encrypt(document As Vintasoft.Imaging.Pdf.PdfDocument, userPassword As String, ownerPassword As String, allowPrint As Boolean, allowExtractContent As Boolean)
' encryption algorithm and encryption key length
Dim algorithm As Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm = Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4
Dim keyLength As Integer = 40
' select user access permissions
Dim permissions As Vintasoft.Imaging.Pdf.Security.UserAccessPermissions = Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.None
permissions = permissions Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.FillInteractiveFormFields
If allowPrint Then
permissions = permissions Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution
End If
If allowExtractContent Then
permissions = permissions Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.ExtractTextAndGraphics
End If
' create encryption system
Dim encryption As New Vintasoft.Imaging.Pdf.Security.EncryptionSystem(algorithm, keyLength, userPassword, ownerPassword, permissions)
' pack and ecrypt document
document.Pack(document.Format, encryption)
End Sub
''' <summary>
''' Changes user and owner password.
''' </summary>
Public Shared Sub ChangePassword(document As Vintasoft.Imaging.Pdf.PdfDocument, oldPassword As String, newUserPassword As String, newOwnerPassword As String)
If Not document.IsEncrypted Then
Throw New System.ArgumentException("Document is not encrypted!")
End If
' authentication
Authenticate(document, oldPassword)
' create new encryption system
Dim encryption As New Vintasoft.Imaging.Pdf.Security.EncryptionSystem(document.EncryptionSystem.Algorithm, document.EncryptionSystem.KeyLength, newUserPassword, newOwnerPassword, document.EncryptionSystem.UserPermissions)
' pack and ecrypt document
document.Pack(document.Format, encryption)
End Sub