Encrypt PDF Document and Set Permissions – C#/VB.NET

Sometimes, we may find that our documents which are submitted to internet are edited by others, even someone steal it and write his/her name as author in documents. It lets us depressed. Therefore, it is necessary to protect our documents. In this post, I will introduce one method to encrypt PDF by C#/VB.NET.

How to protect document? The frequently used method is to encrypt it. Owner gives a password for document. People, including owner must use correct password to open document. Also, owner can set permissions for document, for example, allowing to printing.

In my example, I prepare a PDF document. I want to encrypt it and the password for me and others are different. After opening it, I will set other two permissions, allowed to printing and filling fields.

Then, let’s go to steps. Because I use Spire.PDF in this method, so I add its dll file as reference at the beginning.

  1. Load PDF document from my computer.
  2. Encrypt PDF. Firstly, set password length. Secondly, set owners password and others password. Thirdly, set permissions.
  3. Save and launch new document.

Main Coding:

C#

using System;

using Spire.Pdf;

using Spire.Pdf.Security;

namespace EncryptPDF

{

    class Encryption

    {

        static void Main(string[] args)

        {

            //Load File

            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(@”D:\work\My Documents\Shelley.pdf”);

            //encrypt

            doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;

            doc.Security.OwnerPassword = “123456”;

            doc.Security.UserPassword = “654321”;

            doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;

            //Save and Launch File

            doc.SaveToFile(“Encryption.pdf”);

            doc.Close();

            System.Diagnostics.Process.Start(“Encryption.pdf”);

        }

    }

}

VB.NET

Imports System

Imports Spire.Pdf

Imports Spire.Pdf.Security

Namespace EncryptPDF

       Friend Class Encryption

              Shared Sub Main(ByVal args() As String)

                     ‘Load File

                     Dim doc As New PdfDocument()

                     doc.LoadFromFile(“D:\work\My Documents\Shelley.pdf”)

                     ‘encrypt

                     doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit

                     doc.Security.OwnerPassword = “123456”

                     doc.Security.UserPassword = “654321”

                     doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.FillFields

                     ‘Save and Launch File

                     doc.SaveToFile(“Encryption.pdf”)

                     doc.Close()

                     System.Diagnostics.Process.Start(“Encryption.pdf”)

              End Sub

       End Class

End Namespace

Result Shown by Following:

___________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

2 thoughts on “Encrypt PDF Document and Set Permissions – C#/VB.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s