VintaSoft Imaging .NET SDK v8.6
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / Pack Method / Pack(Stream,PdfFormat,EncryptionSystem) Method
Stream to pack PDF document.
Format of PDF document.
Encryption settings of PDF document.
Example



In This Topic
    Pack(Stream,PdfFormat,EncryptionSystem) Method
    In This Topic
    Packs and saves PDF document to specified stream in specified format and switches to the specified stream.
    Syntax
    'Declaration
     
    Public Overloads Sub Pack( _
       ByVal stream As Stream, _
       ByVal format As PdfFormat, _
       ByVal encryptionSystem As EncryptionSystem _
    ) 
    public void Pack( 
       Stream stream,
       PdfFormat format,
       EncryptionSystem encryptionSystem
    )
    public: void Pack( 
       Stream* stream,
       PdfFormat* format,
       EncryptionSystem* encryptionSystem
    ) 
    public:
    void Pack( 
       Stream^ stream,
       PdfFormat^ format,
       EncryptionSystem^ encryptionSystem
    ) 

    Parameters

    stream
    Stream 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 stream, converts content of the specified stream to specified format, changes encryption settings, removes unused objects from the specified stream and switches to specified stream.
    Source PDF document is not affected. All further changes will affect the specified stream 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: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also