How To Set And Remove PDF Document Security In C#

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

  1. //Create a PDF document  
  2. PdfDocument pdf = new PdfDocument();  
  3.   
  4. //Add a page  
  5. PdfPageBase page = pdf.Pages.Add();  
  6.   
  7. //Insert text  
  8. page.Canvas.DrawString("This document is protected with user password "new PdfFont(PdfFontFamily.Helvetica, 13f), PdfBrushes.Black, PointF.Empty);  
  9.   
  10. //Specify encryption level (algorithm)  
  11. pdf.Security.KeySize = PdfEncryptionKeySize.Key40Bit;  
  12.   
  13. //Add user password  
  14. pdf.Security.UserPassword = "userpassword";  
  15.   
  16. //Save the document  
  17. pdf.SaveToFile("Userpassword.pdf");  

Result

Owner password

  1. //Create a PDF document  
  2. PdfDocument pdf = new PdfDocument();  
  3.   
  4. //Add a page  
  5. PdfPageBase page = pdf.Pages.Add();  
  6.   
  7. //Insert text  
  8. page.Canvas.DrawString("This document is protected with owner password"new PdfFont(PdfFontFamily.Helvetica, 13f), PdfBrushes.Black, new RectangleF(0, 0, 480, 300));  
  9.   
  10. //Specify encryption level (algorithm)  
  11. pdf.Security.KeySize = PdfEncryptionKeySize.Key128Bit;  
  12.   
  13. //Add owner password  
  14. pdf.Security.OwnerPassword = "ownerpassword";  
  15.   
  16. //Save the document  
  17. 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. 

  1. //Create a PDF document  
  2. PdfDocument pdf = new PdfDocument();  
  3.   
  4. //Add a page  
  5. PdfPageBase page = pdf.Pages.Add();  
  6.   
  7. //Insert text  
  8. 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) );  
  9.   
  10. //Specify encryption level (algorithm)  
  11. pdf.Security.KeySize = PdfEncryptionKeySize.Key128Bit;  
  12.   
  13. //Add owner password  
  14. pdf.Security.OwnerPassword = "ownerpassword";  
  15.   
  16. //Set permissions  
  17. pdf.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FullQualityPrint | PdfPermissionsFlags.CopyContent;  
  18.   
  19. //Save the document  
  20. 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

  1. //Load the PDF document with password  
  2. PdfDocument pdf = new PdfDocument("Userpassword.pdf""userpassword");  
  3.   
  4. //Change password  
  5. pdf.Security.UserPassword = "newpassword";  
  6.   
  7. //Save the document  
  8. pdf.SaveToFile("ChangePassword.pdf");  

Changing Permissions

  1. //Load the PDF document with password  
  2. PdfDocument pdf = new PdfDocument("Permissions.pdf""ownerpassword");  
  3.   
  4. //Change the permissions  
  5. pdf.Security.Permissions = PdfPermissionsFlags.FillFields;  
  6.   
  7. //Save the document  
  8. 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.

  1. //Load the PDF document with password  
  2. PdfDocument pdf = new PdfDocument("Userpassword.pdf""userpassword");  
  3.   
  4. //Remove user password  
  5. pdf.Security.UserPassword = string.Empty;  
  6.   
  7. //Save the document  
  8. pdf.SaveToFile("NoUserPassword.pdf");  

Removing owner password from the encrypted PDF document is given below.

  1. //Load the PDF document with password  
  2. PdfDocument pdf = new PdfDocument("Permissions.pdf""ownerpassword");  
  3.   
  4. //Remove owner password  
  5. pdf.Security.OwnerPassword = string.Empty;  
  6.   
  7. //Reset the permissions to default  
  8. pdf.Security.Permissions = PdfPermissionsFlags.Default;  
  9.   
  10. //Save the document  
  11. 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.

  1. //Load the PDF document  
  2. PdfDocument pdf = new PdfDocument();  
  3. pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\Encrypted.pdf");  
  4.   
  5. //Detect if it’s password encrypted  
  6. bool isEncrypted = pdf.IsEncrypted;  
  7. Console.WriteLine(isEncrypted);  

Result

 

Ebook Download
View all
Learn
View all