VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
In This Topic
PDF: Protect PDF document
In This Topic
VintaSoft PDF .NET Plug-in allows to:

Encryption system of PDF document

EncryptionSystem class is intended for storing the cryptosystem parameters. The class contains the following parameters:
An instance of EncryptionSystem class can be created using the constructor:

For detailed information about encryption parameters, difference between user and owner password please refer to PDF Reference sections: 3.5.Encryption, 3.5.2.Standard Security Handler.


Create secured PDF document

New secured PDF document can be created using one of the following constructors of PdfDocument class:
Here is C#/VB.NET code that demonstrates how to create a new secured PDF document.
namespace UserGuide.Programming.Pdf.Encryption
{
    class CreateEncryptedPdfDocument
    {
        public static void CreateDocument(string filename, string userPassword, string ownerPassword)
        {
            // specifies user access permissions
            Vintasoft.Imaging.Pdf.Security.UserAccessPermissions userPermissions = 
                Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution | 
                Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution;
            // create encryption system
            Vintasoft.Imaging.Pdf.Security.EncryptionSystem documentEncryption =
                new Vintasoft.Imaging.Pdf.Security.EncryptionSystem(
                    Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 40, 
                    userPassword, ownerPassword, userPermissions);
            // create document
            using (Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(
                    Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14, documentEncryption))
            {
                // add empty page
                Vintasoft.Imaging.Pdf.Tree.PdfPage page = 
                    document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4);
                // draw text on page
                using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = page.GetGraphics())
                {
                    Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
                        Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesBold);
                    System.Drawing.PointF location = new System.Drawing.PointF(0, page.MediaBox.Height / 2);
                    string text = document.EncryptionSystem.ToString();
                    g.DrawString(text, font, 16, 
                        new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black), location);
                }
                // save document
                document.SaveChanges(filename);
            }
        }
    }
}
Namespace UserGuide.Programming.Pdf.Encryption
    Class CreateEncryptedPdfDocument
        Public Shared Sub CreateDocument(filename As String, userPassword As String, ownerPassword As String)
            ' specifies user access permissions
            Dim userPermissions As Vintasoft.Imaging.Pdf.Security.UserAccessPermissions = Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution
            ' create encryption system
            Dim documentEncryption As New Vintasoft.Imaging.Pdf.Security.EncryptionSystem(Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 40, userPassword, ownerPassword, userPermissions)
            ' create document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14, documentEncryption)
                ' add empty page
                Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)
                ' draw text on page
                Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
                    Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesBold)
                    Dim location As New System.Drawing.PointF(0, page.MediaBox.Height / 2)
                    Dim text As String = document.EncryptionSystem.ToString()
                    g.DrawString(text, font, 16, New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black), location)
                End Using
                ' save document
                document.SaveChanges(filename)
            End Using
        End Sub
    End Class
End Namespace



Load secured PDF document

Secured PDF document can be loaded by 2 ways:
Here is C#/VB.NET code that demonstrates how to load a PDF document using ImageCollection class.
namespace UserGuide.Programming.Pdf.Encryption
{
    class OpenUsingImageCollection
    {
        public static void AddPdfDocumentToImageCollection(
            string filename, Vintasoft.Imaging.ImageCollection images)
        {
            Vintasoft.Imaging.Pdf.PdfDocumentController.AuthenticateRequest += 
                new System.EventHandler<Vintasoft.Imaging.Pdf.PdfDocumentEventArgs>(PdfDocumentController_AuthenticateRequest);
            images.Add(filename);
        }

        static void PdfDocumentController_AuthenticateRequest(
            object sender, Vintasoft.Imaging.Pdf.PdfDocumentEventArgs e)
        {
            Vintasoft.Imaging.Pdf.PdfDocument document = e.Document;
            while (true)
            {
                System.Console.Write("Enter password: ");
                // get password string
                string password = System.Console.ReadLine();
                // performs authentication
                Vintasoft.Imaging.Pdf.Security.AuthorizationResult authorization = 
                    document.Authenticate(password);
                // check authorization result
                if (authorization == 
                    Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                {
                    System.Console.WriteLine("The password is incorrect.");
                }
                else
                {
                    System.Console.WriteLine(authorization.ToString());
                    return;
                }
            }
        }
    }
}
Namespace UserGuide.Programming.Pdf.Encryption
    Class OpenUsingImageCollection
        Public Shared Sub AddPdfDocumentToImageCollection(filename As String, images As Vintasoft.Imaging.ImageCollection)
            AddHandler Vintasoft.Imaging.Pdf.PdfDocumentController.AuthenticateRequest, New System.EventHandler(Of Vintasoft.Imaging.Pdf.PdfDocumentEventArgs)(AddressOf PdfDocumentController_AuthenticateRequest)
            images.Add(filename)
        End Sub

        Private Shared Sub PdfDocumentController_AuthenticateRequest(sender As Object, e As Vintasoft.Imaging.Pdf.PdfDocumentEventArgs)
            Dim document As Vintasoft.Imaging.Pdf.PdfDocument = e.Document
            While True
                System.Console.Write("Enter password: ")
                ' get password string
                Dim password As String = System.Console.ReadLine()
                ' performs authentication
                Dim authorization As Vintasoft.Imaging.Pdf.Security.AuthorizationResult = document.Authenticate(password)
                ' check authorization result
                If authorization = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                    System.Console.WriteLine("The password is incorrect.")
                Else
                    System.Console.WriteLine(authorization.ToString())
                    Return
                End If
            End While
        End Sub
    End Class
End Namespace



Here is C#/VB.NET code that demonstrates how to load a PDF document using PdfDocument class, for case, when it is known beforehand that the PDF document is secured.
namespace UserGuide.Programming.Pdf.Encryption
{
    class OpenEncryptedPdfDocument
    {
        public static Vintasoft.Imaging.Pdf.PdfDocument Open(string filename)
        {
            Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(filename);
            while (true)
            {
                System.Console.Write("Enter password: ");
                // get password string
                string password = System.Console.ReadLine();
                // performs authentication
                Vintasoft.Imaging.Pdf.Security.AuthorizationResult authorization = 
                    document.Authenticate(password);
                // check authorization result
                if (authorization == 
                    Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                {
                    System.Console.WriteLine("The password is incorrect.");
                }
                else
                {
                    System.Console.WriteLine(authorization.ToString());
                    break;
                }
            }
            return document;
        }
    }
}
Namespace UserGuide.Programming.Pdf.Encryption
    Class OpenEncryptedPdfDocument
        Public Shared Function Open(filename As String) As Vintasoft.Imaging.Pdf.PdfDocument
            Dim document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
            While True
                System.Console.Write("Enter password: ")
                ' get password string
                Dim password As String = System.Console.ReadLine()
                ' performs authentication
                Dim authorization As Vintasoft.Imaging.Pdf.Security.AuthorizationResult = document.Authenticate(password)
                ' check authorization result
                If authorization = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                    System.Console.WriteLine("The password is incorrect.")
                Else
                    System.Console.WriteLine(authorization.ToString())
                    Exit While
                End If
            End While
            Return document
        End Function
    End Class
End Namespace



Here is C#/VB.NET code that demonstrates how to load a PDF document using PdfDocument class, for case, when it is not known beforehand that the PDF document is secured.
namespace UserGuide.Programming.Pdf.Encryption
{
    class OpenPdfDocument
    {
        public static Vintasoft.Imaging.Pdf.PdfDocument Open(string filename)
        {
            Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(filename);
            if (document.IsEncrypted)
            {
                while (true)
                {
                    System.Console.Write("Enter password: ");
                    // get password string
                    string password = System.Console.ReadLine();
                    // performs authentication
                    Vintasoft.Imaging.Pdf.Security.AuthorizationResult authorization = 
                        document.Authenticate(password);
                    // check authorization result
                    if (authorization == Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                    {
                        System.Console.WriteLine("The password is incorrect.");
                    }
                    else
                    {
                        System.Console.WriteLine(authorization.ToString());
                        break;
                    }
                }
            }
            return document;
        }
    }
}
Namespace UserGuide.Programming.Pdf.Encryption
    Class OpenPdfDocument
        Public Shared Function Open(filename As String) As Vintasoft.Imaging.Pdf.PdfDocument
            Dim document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
            If document.IsEncrypted Then
                While True
                    System.Console.Write("Enter password: ")
                    ' get password string
                    Dim password As String = System.Console.ReadLine()
                    ' performs authentication
                    Dim authorization As Vintasoft.Imaging.Pdf.Security.AuthorizationResult = document.Authenticate(password)
                    ' check authorization result
                    If authorization = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                        System.Console.WriteLine("The password is incorrect.")
                    Else
                        System.Console.WriteLine(authorization.ToString())
                        Exit While
                    End If
                End While
            End If
            Return document
        End Function
    End Class
End Namespace



Secure an existing PDF document

For securing an existing PDF document the document must be packed using cryptosystem. This can be done using one of the following PdfDocument.Pack methods:
Here is C#/VB.NET code that demonstrates how to secure an existing PDF document.
namespace UserGuide.Programming.Pdf.Encryption
{
    class EncryptExistingPdfDocument
    {
        public static void Encrypt(string filename, string userPassword, string ownerPassword)
        {
            // specifies user access permissions
            Vintasoft.Imaging.Pdf.Security.UserAccessPermissions userPermissions =
                Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution |
                Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution;
            // create encryption system
            Vintasoft.Imaging.Pdf.Security.EncryptionSystem documentEncryption =
                new Vintasoft.Imaging.Pdf.Security.EncryptionSystem(
                    Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 40, 
                    userPassword, ownerPassword, userPermissions);
            // open PDF document
            using (Vintasoft.Imaging.Pdf.PdfDocument document =
                new Vintasoft.Imaging.Pdf.PdfDocument(filename))
            {
                // encrypt PDF document
                document.Pack(Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14, documentEncryption);
            }
        }
    }
}

Namespace UserGuide.Programming.Pdf.Encryption
    Class EncryptExistingPdfDocument
        Public Shared Sub Encrypt(filename As String, userPassword As String, ownerPassword As String)
            ' specifies user access permissions
            Dim userPermissions As Vintasoft.Imaging.Pdf.Security.UserAccessPermissions = Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInHighResolution Or Vintasoft.Imaging.Pdf.Security.UserAccessPermissions.PrintDocumentInLowResolution
            ' create encryption system
            Dim documentEncryption As New Vintasoft.Imaging.Pdf.Security.EncryptionSystem(Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 40, userPassword, ownerPassword, userPermissions)
            ' open PDF document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
                ' encrypt PDF document
                document.Pack(Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14, documentEncryption)
            End Using
        End Sub
    End Class
End Namespace



Decode an existing secured PDF document

For decoding an existing secured PDF document the document must be packed without the use of cryptosystem.

Here is C#/VB.NET code that demonstrates how to convert an existing secured PDF document to a not secured PDF document.
namespace UserGuide.Programming.Pdf.Encryption
{
    class DecryptPdfDocument
    {
        public static void Decrypt(string filename, string password)
        {
            // open document
            using (Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(filename))
            {
                // authenticate
                if (document.Authenticate(password) == Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                    throw new System.Exception("Incorrect password.");
                // decrypt document
                Vintasoft.Imaging.Pdf.Security.EncryptionSystem newEncryption = null;
                document.Pack(document.Format, newEncryption);
            }
        }
    }
}

Namespace UserGuide.Programming.Pdf.Encryption
    Class DecryptPdfDocument
        Public Shared Sub Decrypt(filename As String, password As String)
            ' open document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
                ' authenticate
                If document.Authenticate(password) = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                    Throw New System.Exception("Incorrect password.")
                End If
                ' decrypt document
                Dim newEncryption As Vintasoft.Imaging.Pdf.Security.EncryptionSystem = Nothing
                document.Pack(document.Format, newEncryption)
            End Using
        End Sub
    End Class
End Namespace



Change the security parameters and access permission rights of PDF document

For changing PDF document security parameter (encryption type, password, access permissions) the document must packed using cryptosystem. This can be done using one of the following PdfDocument.Pack methods:
Here is C#/VB.NET code that demonstrates how to change the user access permission parameters of PDF document.
namespace UserGuide.Programming.Pdf.Encryption
{
    class ChangeUserAccessPermissions
    {
        public static void Change(string filename, 
            string userPassword, string ownerPassword, 
            Vintasoft.Imaging.Pdf.Security.UserAccessPermissions newUserPermissions)
        {
            using(Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(filename))
            {
                if (document.IsEncrypted)
                {
                    document.Authenticate(ownerPassword);
                    if (document.AuthorizationResult == Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                        document.Authenticate(userPassword);
                    if (document.AuthorizationResult == Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                        throw new System.Exception("Incorrect password.");
                }
                Vintasoft.Imaging.Pdf.Security.EncryptionSystem newEnctyptionSystem = 
                    new Vintasoft.Imaging.Pdf.Security.EncryptionSystem(
                        Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 
                        40, userPassword, ownerPassword, newUserPermissions);
                document.Pack(document.Format, newEnctyptionSystem);
            }
        }
    }
}

Namespace UserGuide.Programming.Pdf.Encryption
    Class ChangeUserAccessPermissions
        Public Shared Sub Change(filename As String, userPassword As String, ownerPassword As String, newUserPermissions As Vintasoft.Imaging.Pdf.Security.UserAccessPermissions)
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
                If document.IsEncrypted Then
                    document.Authenticate(ownerPassword)
                    If document.AuthorizationResult = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                        document.Authenticate(userPassword)
                    End If
                    If document.AuthorizationResult = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                        Throw New System.Exception("Incorrect password.")
                    End If
                End If
                Dim newEnctyptionSystem As New Vintasoft.Imaging.Pdf.Security.EncryptionSystem(Vintasoft.Imaging.Pdf.Security.EncryptionAlgorithm.RC4, 40, userPassword, ownerPassword, newUserPermissions)
                document.Pack(document.Format, newEnctyptionSystem)
            End Using
        End Sub
    End Class
End Namespace



Edit and save secured PDF document

The secured PDF document can be saved using PdfDocument.Save and PdfDocument.SaveChanges methods.

Here is C#/VB.NET code that demonstrates how to add a page into an existing secured PDF document.
namespace UserGuide.Programming.Pdf.Encryption
{
    class AddPageToEncryptedPdfDocument
    {
        public static void AddPage(string documentFilename, string newPageImageFilename)
        {
            // open PDF document
            using (Vintasoft.Imaging.Pdf.PdfDocument document = Open(documentFilename))
            {
                // add page
                using (Vintasoft.Imaging.VintasoftImage image = 
                    new Vintasoft.Imaging.VintasoftImage(newPageImageFilename))
                    document.Pages.Add(image);
                // save changes
                document.SaveChanges();
            }
        }

        private static Vintasoft.Imaging.Pdf.PdfDocument Open(string filename)
        {
            Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(filename);
            if (document.IsEncrypted)
            {
                while (true)
                {
                    System.Console.Write("Enter password: ");
                    // get password string
                    string password = System.Console.ReadLine();
                    // performs authentication
                    Vintasoft.Imaging.Pdf.Security.AuthorizationResult authorization = 
                        document.Authenticate(password);
                    // check authorization result
                    if (authorization == Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword)
                    {
                        System.Console.WriteLine("The password is incorrect.");
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return document;
        }

    }
}
Namespace UserGuide.Programming.Pdf.Encryption
    Class AddPageToEncryptedPdfDocument
        Public Shared Sub AddPage(documentFilename As String, newPageImageFilename As String)
            ' open PDF document
            Using document As Vintasoft.Imaging.Pdf.PdfDocument = Open(documentFilename)
                ' add page
                Using image As New Vintasoft.Imaging.VintasoftImage(newPageImageFilename)
                    document.Pages.Add(image)
                End Using
                ' save changes
                document.SaveChanges()
            End Using
        End Sub

        Private Shared Function Open(filename As String) As Vintasoft.Imaging.Pdf.PdfDocument
            Dim document As New Vintasoft.Imaging.Pdf.PdfDocument(filename)
            If document.IsEncrypted Then
                While True
                    System.Console.Write("Enter password: ")
                    ' get password string
                    Dim password As String = System.Console.ReadLine()
                    ' performs authentication
                    Dim authorization As Vintasoft.Imaging.Pdf.Security.AuthorizationResult = document.Authenticate(password)
                    ' check authorization result
                    If authorization = Vintasoft.Imaging.Pdf.Security.AuthorizationResult.IncorrectPassword Then
                        System.Console.WriteLine("The password is incorrect.")
                    Else
                        Exit While
                    End If
                End While
            End If
            Return document
        End Function

    End Class
End Namespace