Docotic.Pdf Library Help > Samples > Security > Protect PDF document with AES

This sample shows how to protect your PDF document with a password using AES 256-bit encryption algorithm.

Specify PdfEncryptionAlgorithm.Aes256Bit for PdfDocument.Encryption property and setup owner, user or both passwords to protect output PDF file with advanced protection offered by AES 256-bit encryption.

C#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class ProtectDocumentWithAes
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "ProtectDocumentWithAes.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.Pages[0].Canvas.DrawString("Hello World!");

                pdf.UserPassword = "user";
                pdf.Encryption = PdfEncryptionAlgorithm.Aes256Bit;

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class ProtectDocumentWithAes
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "ProtectDocumentWithAes.pdf"

            Using pdf As New PdfDocument()
                pdf.Pages(0).Canvas.DrawString("Hello World!")

                pdf.UserPassword = "user"
                pdf.Encryption = PdfEncryptionAlgorithm.Aes256Bit

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace