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.
- Load PDF document from my computer.
- Encrypt PDF. Firstly, set password length. Secondly, set owners password and others password. Thirdly, set permissions.
- Save and launch new document.
static void Main(string args)
PdfDocument doc = new PdfDocument();
doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
doc.Security.OwnerPassword = "123456";
doc.Security.UserPassword = "654321";
doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;
//Save and Launch File
Friend Class Encryption
Shared Sub Main(ByVal args() As String)
Dim doc As New PdfDocument()
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
Result Shown by Following:
Click Here to Learn more about Spire.PDF
Click Here to Download Spire.PDF