Background
Nowadays, data theft has become more and more popular. Thus, the security of our confidential information has vaulted to the top of our concerns. For example, we will automatically think to secure an important file before we send it to recipients to prevent its content from an unauthorized access. And this is the main purpose of writing this article.
Introduction
In this blog, I will introduce how to set and remove PDF document security in C#, using a
.NET PDF component. The contents of this blog can be summarized, as shown below.
- Setting security to PDF document.
- Changing security settings of PDF document.
- Removing security from PDF document.
- Detecting if the PDF document is encrypted.
Using the code
Part 1 - Setting security to PDF document
Adding passwords
We can add two kinds of passwords to protect PDF documents, i.e. we can add a user password (also referred to as document open password), so that only users who have the password can open the document and we can add an owner password (also referred to as permissions password), so that only users, who have the password can change the permissions of the document.
User password
-
- PdfDocument pdf = new PdfDocument();
-
-
- PdfPageBase page = pdf.Pages.Add();
-
-
- page.Canvas.DrawString("This document is protected with user password ", new PdfFont(PdfFontFamily.Helvetica, 13f), PdfBrushes.Black, PointF.Empty);
-
-
- pdf.Security.KeySize = PdfEncryptionKeySize.Key40Bit;
-
-
- pdf.Security.UserPassword = "userpassword";
-
-
- pdf.SaveToFile("Userpassword.pdf");
Result
Owner password
-
- PdfDocument pdf = new PdfDocument();
-
-
- PdfPageBase page = pdf.Pages.Add();
-
-
- page.Canvas.DrawString("This document is protected with owner password", new PdfFont(PdfFontFamily.Helvetica, 13f), PdfBrushes.Black, new RectangleF(0, 0, 480, 300));
-
-
- pdf.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
-
-
- pdf.Security.OwnerPassword = "ownerpassword";
-
-
- pdf.SaveToFile("Ownerpassword.pdf");
Result
Setting Permissions
Permissions can increase the flexibility of document security. By customizing the permission settings, we can easily allow or prevent users from editing and copying information or limit other activities like printing and form filling.
-
- PdfDocument pdf = new PdfDocument();
-
-
- PdfPageBase page = pdf.Pages.Add();
-
-
- page.Canvas.DrawString("This document is protected with owner password, and the following permissions are granted when the document is opened: Print, FullQualityPrint, CopyContent", new PdfFont(PdfFontFamily.Helvetica, 13f), PdfBrushes.Black,new RectangleF(0,0,480,300) );
-
-
- pdf.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
-
-
- pdf.Security.OwnerPassword = "ownerpassword";
-
-
- pdf.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FullQualityPrint | PdfPermissionsFlags.CopyContent;
-
-
- pdf.SaveToFile("Permissions.pdf");
Result
Part 2
Changing security settings of PDF document
After setting security, we can change the security settings, i.e. password or permissions of PDF document.
Changing Password
-
- PdfDocument pdf = new PdfDocument("Userpassword.pdf", "userpassword");
-
-
- pdf.Security.UserPassword = "newpassword";
-
-
- pdf.SaveToFile("ChangePassword.pdf");
Changing Permissions
-
- PdfDocument pdf = new PdfDocument("Permissions.pdf", "ownerpassword");
-
-
- pdf.Security.Permissions = PdfPermissionsFlags.FillFields;
-
-
- pdf.SaveToFile("ChangePermissions.pdf");
Part 3
Removing Security from PDF document
If we know the password of a PDF document, we can remove the security from the document and make it available for editing.
Removing user password from the encrypted PDF document is given below.
-
- PdfDocument pdf = new PdfDocument("Userpassword.pdf", "userpassword");
-
-
- pdf.Security.UserPassword = string.Empty;
-
-
- pdf.SaveToFile("NoUserPassword.pdf");
Removing owner password from the encrypted PDF document is given below.
-
- PdfDocument pdf = new PdfDocument("Permissions.pdf", "ownerpassword");
-
-
- pdf.Security.OwnerPassword = string.Empty;
-
-
- pdf.Security.Permissions = PdfPermissionsFlags.Default;
-
-
- pdf.SaveToFile("NoOwnerPassword.pdf");
Part 4
Detecting if the PDF document is encrypted
Sometimes, we may want to detect whether the PDF document is encrypted or not before we open it.
-
- PdfDocument pdf = new PdfDocument();
- pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\Encrypted.pdf");
-
-
- bool isEncrypted = pdf.IsEncrypted;
- Console.WriteLine(isEncrypted);
Result