VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / Pack Methods / Pack(String,PdfFormat,EncryptionSystem) Method
Syntax Remarks Example Requirements SeeAlso
In This Topic
Pack(String,PdfFormat,EncryptionSystem) Method (PdfDocument)
In This Topic
Packs and saves PDF document to specified file in specified format and switches to the specified file.
Syntax
'Declaration

Public Overloads Sub Pack( _
ByVal filename
File name to pack PDF document.
As System.String, _
ByVal format
Format of PDF document.
As PdfFormat, _
ByVal encryptionSystem
Encryption settings of PDF document.
As Vintasoft.Imaging.Pdf.Security.EncryptionSystem _
)

Parameters

filename
File name to pack PDF document.
format
Format of PDF document.
encryptionSystem
Encryption settings of PDF document.
Remarks

This methods copies content of this PDF document to specified file, converts content of the specified file to specified format, changes encryption settings, removes unused objects from the specified file and switches to specified file.
Source PDF document is not affected. All further changes will affect the specified file only.

Example


''' <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


/// <summary>
/// Performs the authentication using the specified password.
/// </summary>
public static void Authenticate(Vintasoft.Imaging.Pdf.PdfDocument document, string password)
{
    if (document.AuthorizationResult == 
        Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
    {
        document.Authenticate(password);
        if (document.AuthorizationResult ==
            Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
        {
            throw new System.ArgumentException("Password is incorrect.");
        }
    }
}

/// <summary>
/// Decrypts the specified document.
/// </summary>
public static void Decrypt(Vintasoft.Imaging.Pdf.PdfDocument document, string password)
{
    // authentication
    Authenticate(document, password);
    // pack document without encryption
    document.Pack(document.Format, null);
}

/// <summary>
/// Encrypts PDF document with specified user password, owner password and
/// user access permissions.
/// </summary>
public static void Encrypt(
    Vintasoft.Imaging.Pdf.PdfDocument document,
    string userPassword,
    string ownerPassword,
    bool allowPrint,
    bool allowExtractContent)
{
    // encryption algorithm and encryption key length
    Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm algorithm = 
        Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4;
    int keyLength = 40;

    // select user access permissions
    Vintasoft.Imaging.Pdf.Security.UserAccessPermissions permissions = 
        Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.None;
    permissions |= Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.FillInteractiveFormFields;
    if (allowPrint)
    {
        permissions |=
               Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution |
               Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution;
    }
    if (allowExtractContent)
    {
        permissions |= Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.ExtractTextAndGraphics;
    }

    // create encryption system
    Vintasoft.Imaging.Pdf.Security.EncryptionSystem encryption = 
        new Vintasoft.Imaging.Pdf.Security.EncryptionSystem(algorithm, keyLength,
            userPassword, ownerPassword, permissions);
    // pack and ecrypt document
    document.Pack(document.Format, encryption);
}

/// <summary>
/// Changes user and owner password.
/// </summary>
public static void ChangePassword(
    Vintasoft.Imaging.Pdf.PdfDocument document,
    string oldPassword,
    string newUserPassword,
    string newOwnerPassword)
{
    if (!document.IsEncrypted)
        throw new System.ArgumentException("Document is not encrypted!");

    // authentication
    Authenticate(document, oldPassword);
    // create new encryption system
    Vintasoft.Imaging.Pdf.Security.EncryptionSystem encryption = 
        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);
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also