# pdfviewer-encryption

> Sample that uses PdfDocument to generate a PDF, applies RC4 or AES encryption with user and owner passwords, then writes the encrypted file.

**Framework:** winforms  **Component:** PdfViewer  **Variant:** encryption

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/winforms/pdfviewer-encryption.json
```

**Install Package(s)**

```bash
dotnet add package Syncfusion.Pdf.Windows
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/pdfviewer-encryption/pdfviewer.Designer.cs

```cs
using System.Drawing;
using System.Windows.Forms;

namespace Encryption
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.rdButton256BitRevision6 = new System.Windows.Forms.RadioButton();
            this.rdButton256Bit = new System.Windows.Forms.RadioButton();
            this.rdButton128Bit = new System.Windows.Forms.RadioButton();
            this.rdButton40Bit = new System.Windows.Forms.RadioButton();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.rdButtonAESGCM = new System.Windows.Forms.RadioButton();
            this.rdButtonAES = new System.Windows.Forms.RadioButton();
            this.rdButtonRC4 = new System.Windows.Forms.RadioButton();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.cmbEncrypt = new System.Windows.Forms.ComboBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.BackColor = System.Drawing.Color.Transparent;
            this.button1.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
            this.button1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
            this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.button1.Location = new System.Drawing.Point(396, 432);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(100, 28);
            this.button1.TabIndex = 0;
            this.button1.Text = "PDF";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // label1
            //
            this.label1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.label1.Location = new System.Drawing.Point(13, 111);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(507, 87);
            this.label1.TabIndex = 1;
            this.label1.Text = "Click the button below to view a PDF created with Essential PDF. Please note that Adobe Reader or its equivalent is required to view the resultant document. However, AES-GCM encryption is not supported by Adobe Reader to view such documents, use Foxit Reader or another compatible PDF viewer.";
            //
            // pictureBox1
            //
            this.pictureBox1.Image = System.Drawing.Image.FromFile(EncryptionDataSource.GetImagePath(EncryptionDataSource.HeaderImageFileName));
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(510, 110);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 24;
            this.pictureBox1.TabStop = false;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(15, 21);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(219, 17);
            this.label2.TabIndex = 25;
            this.label2.Text = "Owner password : syncfusion";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(16, 47);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(208, 17);
            this.label3.TabIndex = 26;
            this.label3.Text = "User password   : password";
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Location = new System.Drawing.Point(16, 366);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(272, 75);
            this.groupBox1.TabIndex = 27;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Encryption Password";
            //
            // groupBox2
            //
            this.groupBox2.Controls.Add(this.rdButton256BitRevision6);
            this.groupBox2.Controls.Add(this.rdButton256Bit);
            this.groupBox2.Controls.Add(this.rdButton128Bit);
            this.groupBox2.Controls.Add(this.rdButton40Bit);
            this.groupBox2.Location = new System.Drawing.Point(16, 200);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(272, 82);
            this.groupBox2.TabIndex = 28;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Key Size";
            //
            // rdButton256BitRevision6
            //
            this.rdButton256BitRevision6.AutoSize = true;
            this.rdButton256BitRevision6.Location = new System.Drawing.Point(8, 53);
            this.rdButton256BitRevision6.Name = "rdButton256BitRevision6";
            this.rdButton256BitRevision6.Size = new System.Drawing.Size(133, 20);
            this.rdButton256BitRevision6.TabIndex = 3;
            this.rdButton256BitRevision6.TabStop = true;
            this.rdButton256BitRevision6.Text = "256 Bit Revision 6";
            this.rdButton256BitRevision6.UseVisualStyleBackColor = true;
            this.rdButton256BitRevision6.CheckedChanged += new System.EventHandler(this.rdButton256BitRevision6_CheckedChanged);
            //
            // rdButton256Bit
            //
            this.rdButton256Bit.AutoSize = true;
            this.rdButton256Bit.Location = new System.Drawing.Point(189, 53);
            this.rdButton256Bit.Name = "rdButton256Bit";
            this.rdButton256Bit.Size = new System.Drawing.Size(64, 20);
            this.rdButton256Bit.TabIndex = 2;
            this.rdButton256Bit.TabStop = true;
            this.rdButton256Bit.Text = "256Bit";
            this.rdButton256Bit.UseVisualStyleBackColor = true;
            this.rdButton256Bit.CheckedChanged += new System.EventHandler(this.rdButton256Bit_CheckedChanged);
            //
            // rdButton128Bit
            //
            this.rdButton128Bit.AutoSize = true;
            this.rdButton128Bit.Checked = true;
            this.rdButton128Bit.Location = new System.Drawing.Point(189, 23);
            this.rdButton128Bit.Name = "rdButton128Bit";
            this.rdButton128Bit.Size = new System.Drawing.Size(67, 20);
            this.rdButton128Bit.TabIndex = 1;
            this.rdButton128Bit.TabStop = true;
            this.rdButton128Bit.Text = "128 Bit";
            this.rdButton128Bit.UseVisualStyleBackColor = true;
            this.rdButton128Bit.CheckedChanged += new System.EventHandler(this.rdButton128Bit_CheckedChanged);
            //
            // rdButton40Bit
            //
            this.rdButton40Bit.AutoSize = true;
            this.rdButton40Bit.Location = new System.Drawing.Point(8, 25);
            this.rdButton40Bit.Name = "rdButton40Bit";
            this.rdButton40Bit.Size = new System.Drawing.Size(60, 20);
            this.rdButton40Bit.TabIndex = 0;
            this.rdButton40Bit.TabStop = true;
            this.rdButton40Bit.Text = "40 Bit";
            this.rdButton40Bit.UseVisualStyleBackColor = true;
            this.rdButton40Bit.CheckedChanged += new System.EventHandler(this.rdButton40Bit_CheckedChanged);
            //
            // groupBox3
            //
            this.groupBox3.Controls.Add(this.rdButtonAESGCM);
            this.groupBox3.Controls.Add(this.rdButtonAES);
            this.groupBox3.Controls.Add(this.rdButtonRC4);
            this.groupBox3.Location = new System.Drawing.Point(312, 200);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(173, 82);
            this.groupBox3.TabIndex = 29;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Encryption Algorithm";
            //
            // rdButtonAESGCM
            //
            this.rdButtonAESGCM.AutoSize = true;
            this.rdButtonAESGCM.Location = new System.Drawing.Point(8, 53);
            this.rdButtonAESGCM.Name = "rdButtonAESGCM";
            this.rdButtonAESGCM.Size = new System.Drawing.Size(85, 20);
            this.rdButtonAESGCM.TabIndex = 2;
            this.rdButtonAESGCM.TabStop = true;
            this.rdButtonAESGCM.Text = "AESGCM";
            this.rdButtonAESGCM.UseVisualStyleBackColor = true;
            this.rdButtonAESGCM.CheckedChanged += new System.EventHandler(this.rdButtonAESGCM_CheckedChanged);
            //
            // rdButtonAES
            //
            this.rdButtonAES.AutoSize = true;
            this.rdButtonAES.Checked = true;
            this.rdButtonAES.Location = new System.Drawing.Point(104, 25);
            this.rdButtonAES.Name = "rdButtonAES";
            this.rdButtonAES.Size = new System.Drawing.Size(55, 20);
            this.rdButtonAES.TabIndex = 1;
            this.rdButtonAES.TabStop = true;
            this.rdButtonAES.Text = "AES";
            this.rdButtonAES.UseVisualStyleBackColor = true;
            this.rdButtonAES.CheckedChanged += new System.EventHandler(this.rdButtonAES_CheckedChanged);
            //
            // rdButtonRC4
            //
            this.rdButtonRC4.AutoSize = true;
            this.rdButtonRC4.Location = new System.Drawing.Point(8, 25);
            this.rdButtonRC4.Name = "rdButtonRC4";
            this.rdButtonRC4.Size = new System.Drawing.Size(54, 20);
            this.rdButtonRC4.TabIndex = 0;
            this.rdButtonRC4.TabStop = true;
            this.rdButtonRC4.Text = "RC4";
            this.rdButtonRC4.UseVisualStyleBackColor = true;
            this.rdButtonRC4.CheckedChanged += new System.EventHandler(this.rdButtonRC4_CheckedChanged);
            //
            // groupBox4
            //
            this.groupBox4.Controls.Add(this.cmbEncrypt);
            this.groupBox4.Location = new System.Drawing.Point(16, 292);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(272, 62);
            this.groupBox4.TabIndex = 31;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Encryption Options ";
            //
            // cmbEncrypt
            //
            this.cmbEncrypt.FormattingEnabled = true;
            this.cmbEncrypt.Items.AddRange(new object[] {
            "Encrypt all contents",
            "Encrypt all contents except metadata",
            "Encrypt only attachments"});
            this.cmbEncrypt.Location = new System.Drawing.Point(3, 23);
            this.cmbEncrypt.Name = "cmbEncrypt";
            this.cmbEncrypt.Size = new System.Drawing.Size(265, 24);
            this.cmbEncrypt.SelectedIndex = 0;
            this.cmbEncrypt.TabIndex = 0;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(510, 479);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(EncryptionDataSource.GetImagePath(EncryptionDataSource.IconFileName));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Encryption";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.RadioButton rdButton256Bit;
        private System.Windows.Forms.RadioButton rdButton128Bit;
        private System.Windows.Forms.RadioButton rdButton40Bit;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.RadioButton rdButtonAES;
        private System.Windows.Forms.RadioButton rdButtonRC4;
        private System.Windows.Forms.RadioButton rdButton256BitRevision6;
        private System.Windows.Forms.RadioButton rdButtonAESGCM;
        private System.Windows.Forms.GroupBox groupBox4;
        private System.Windows.Forms.ComboBox cmbEncrypt;
    }
}

```

### src/components/pdfviewer-encryption/pdfviewer.cs

```cs
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Security;
using Syncfusion.Windows.Forms;

namespace Encryption
{
    /// <summary>
    /// Demonstrates how to protect a generated PDF document with the
    /// Essential PDF security settings (key size, algorithm, passwords and
    /// encryption options).
    /// </summary>
    public partial class Form1 : MetroForm
    {
        #region Constructor
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.MinimizeBox = true;
        }
        #endregion

        #region Events
        private void button1_Click(object sender, System.EventArgs e)
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.Pages.Add();
            PdfGraphics graphics = page.Graphics;

            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
            PdfBrush brush = PdfBrushes.Black;

            //Document security
            PdfSecurity security = document.Security;

            //Specify key size and encryption algorithm
            if (rdButton40Bit.Checked)
            {
                //use 40 bits key in RC4 mode
                security.KeySize = PdfEncryptionKeySize.Key40Bit;
            }
            else if (rdButton128Bit.Checked && rdButtonRC4.Checked)
            {
                //use 128 bits key in RC4 mode
                security.KeySize = PdfEncryptionKeySize.Key128Bit;
                security.Algorithm = PdfEncryptionAlgorithm.RC4;
            }
            else if (rdButton128Bit.Checked && rdButtonAES.Checked)
            {
                //use 128 bits key in AES mode
                security.KeySize = PdfEncryptionKeySize.Key128Bit;
                security.Algorithm = PdfEncryptionAlgorithm.AES;
            }
            else if (rdButton256Bit.Checked)
            {
                security.KeySize = PdfEncryptionKeySize.Key256Bit;
                if (rdButtonAES.Checked)
                {
                    //use 256 bits key in AES mode
                    security.Algorithm = PdfEncryptionAlgorithm.AES;
                }
                else if (rdButtonAESGCM.Checked)
                {
                    //use 256 bits key in AESGCM mode
                    security.Algorithm = PdfEncryptionAlgorithm.AESGCM;
                    document.FileStructure.Version = PdfVersion.Version2_0;
                }
            }
            else if (rdButton256BitRevision6.Checked)
            {
                security.KeySize = PdfEncryptionKeySize.Key256BitRevision6;
                security.Algorithm = PdfEncryptionAlgorithm.AES;
            }
            if (cmbEncrypt.SelectedIndex == 0 || !cmbEncrypt.Enabled)
                security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContents;
            else if (cmbEncrypt.SelectedIndex == 1)
                security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;
            else if (cmbEncrypt.SelectedIndex == 2)
            {
                security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
                //Read the file
                string attachmentPath = EncryptionDataSource.GetDataPath(EncryptionDataSource.AttachmentFileName);
                if (File.Exists(attachmentPath))
                {
                    FileStream file = new FileStream(attachmentPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    //Creates an attachment
                    PdfAttachment attachment = new PdfAttachment(EncryptionDataSource.AttachmentFileName, file);

                    attachment.ModificationDate = DateTime.Now;

                    attachment.Description = "About Syncfusion";

                    attachment.MimeType = "application/txt";

                    //Adds the attachment to the document
                    document.Attachments.Add(attachment);
                }
            }

            security.OwnerPassword = EncryptionDataSource.OwnerPassword;
            security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FullQualityPrint;
            security.UserPassword = EncryptionDataSource.UserPassword;

            string text = "Security options:\n\n" + String.Format("KeySize: {0}\n\nEncryption Algorithm: {4}\n\nOwner Password: {1}\n\nPermissions: {2}\n\n" +
                "User Password: {3}", security.KeySize, security.OwnerPassword, security.Permissions, security.UserPassword, security.Algorithm);
            if (rdButton256BitRevision6.Checked)
            {
                text += String.Format("\n\nRevision: {0}", "Revision6");
            }
            else if (rdButton256Bit.Checked && rdButtonAES.Checked)
            {
                text += String.Format("\n\nRevision: {0}", "Revision5");
            }
            else if (rdButton256Bit.Checked && rdButtonAESGCM.Checked)
            {
                text += String.Format("\n\nRevision: {0}", "Revision7");
            }

            graphics.DrawString("Document is Encrypted with following settings", font, brush, PointF.Empty);
            font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16f, PdfFontStyle.Bold);
            graphics.DrawString(text, font, brush, new PointF(0, 40));

            document.Save(EncryptionDataSource.GetOutputPath());
            document.Close();

            //Message box confirmation to view the created PDF document.
            if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                //Launching the PDF file using the default application.
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo(EncryptionDataSource.GetOutputPath())
                {
                    UseShellExecute = true
                };
                process.Start();
            }

            this.Close();
        }

        private void rdButton40Bit_CheckedChanged(object sender, EventArgs e)
        {
            if (rdButton40Bit.Checked)
            {
                rdButtonRC4.Enabled = true;
                rdButtonRC4.Checked = true;
                rdButtonAES.Enabled = false;
                rdButtonAESGCM.Enabled = false;
                cmbEncrypt.Enabled = false;
            }
        }

        private void rdButton128Bit_CheckedChanged(object sender, EventArgs e)
        {
            rdButtonRC4.Enabled = true;
            rdButtonAES.Enabled = true;
            rdButtonAESGCM.Enabled = false;
            cmbEncrypt.Enabled = !rdButtonRC4.Checked;
        }

        private void rdButton256Bit_CheckedChanged(object sender, EventArgs e)
        {
            rdButtonAESGCM.Enabled = true;
            rdButtonAESGCM.Checked = true;
            rdButtonAES.Enabled = true;
            rdButtonAES.Checked = true;
            rdButtonRC4.Enabled = false;
            cmbEncrypt.Enabled = true;
        }

        private void rdButton256BitRevision6_CheckedChanged(object sender, EventArgs e)
        {
            rdButtonAES.Enabled = true;
            rdButtonAES.Checked = true;
            rdButtonAESGCM.Enabled = false;
            rdButtonRC4.Enabled = false;
            cmbEncrypt.Enabled = true;
        }

        private void rdButtonAESGCM_CheckedChanged(object sender, EventArgs e)
        {
            cmbEncrypt.Enabled = rdButtonAESGCM.Checked;
        }

        private void rdButtonAES_CheckedChanged(object sender, EventArgs e)
        {
            cmbEncrypt.Enabled = rdButtonAES.Checked;
        }

        private void rdButtonRC4_CheckedChanged(object sender, EventArgs e)
        {
            cmbEncrypt.Enabled = rdButton40Bit.Checked;
        }
        #endregion
    }
}

```
