VintaSoft Imaging .NET SDK 14.1: Документация для .NET разработчика
Vintasoft.Imaging.Pdf Namespace / PdfDocument Class / Pack Methods / Pack(Stream,PdfFormat,EncryptionSystem) Method
Синтаксис Ремарки Example Требования Смотрите также
    Pack(Stream,PdfFormat,EncryptionSystem) Метод (PdfDocument)
    Упаковывает и сохраняет PDF документ в указанный поток в указанном формате и переключается на указанный поток.
    Синтаксис

    Parameters

    stream
    Поток для упаковки PDF документа.
    format
    Формат PDF документа.
    encryptionSystem
    Настройки шифрования PDF документа.
    Ремарки

    Этот метод копирует содержимое этого PDF документа в указанный поток, преобразует содержимое указанного потока в указанный формат, изменяет настройки шифрования, удаляет неиспользуемые объекты из указанного потока и переключается на указанный поток.
    Исходный PDF документ не затрагивается. Все дальнейшие изменения затронут только указанный поток.
    Метод записывает документ в начало stream и усекает stream в соответствии с записанными данными.

    Пример

    ''' <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);
    }
    
    

    Требования

    Целевые платформы: .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    Смотрите также