Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (141).NET Core  (21)Angular  (38)ASP.NET  (47)ASP.NET Core  (52)ASP.NET MVC  (83)Azure  (26)Blazor  (42)DocIO  (18)Essential JS 2  (64)Essential Studio  (158)Flutter  (37)JavaScript  (132)Microsoft  (95)PDF  (46)React  (29)Succinctly series  (118)Syncfusion  (373)TypeScript  (30)Uno Platform  (2)UWP  (4)Vue  (25)Webinar  (12)Windows Forms  (52)WPF  (85)Xamarin  (102)XlsIO  (19)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (97)Chart  (36)Cloud  (8)Company  (445)Dashboard  (4)Data Science  (3)Data Validation  (2)DataGrid  (25)Development  (232)Doc  (7)eBook  (89)Enterprise  (22)Entity Framework  (4)Essential Tools  (14)Excel  (10)Extensions  (6)File Manager  (3)Gantt  (6)Gauge  (3)Git  (3)Grid  (24)HTML  (8)Installer  (1)Knockout  (2)LINQPad  (1)Linux  (1)M-Commerce  (1)Metro Studio  (11)Mobile  (123)Mobile MVC  (9)OLAP server  (1)Orubase  (12)Partners  (20)PDF viewer  (14)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (5)Road Map  (9)Scheduler  (13)SfDataGrid  (7)Silverlight  (21)Sneak Peek  (12)Solution Services  (2)Spreadsheet  (2)SQL  (3)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (8)Tips and Tricks  (10)UI  (51)Uncategorized  (68)Unix  (2)User interface  (39)Visual State Manager  (1)Visual Studio  (12)Visual Studio Code  (6)Web  (78)What's new  (44)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Encrypt and decrypt PDF using CSharp

Simple Steps to Encrypt and Decrypt PDF Files Using C#

The Syncfusion PDF Library is a .NET PDF Library that allows users to encrypt and decrypt PDF documents in C# and VB.NET.

PDF encryption allows users to secure PDF documents with passwords. Two types of passwords are available:

  • Document-open password: A document-open password, also known as a user password, is used to open a PDF document.
  • Permission password: A permission password, also known as master or owner password, is used to restrict options such as printing, editing, and copying content in a PDF.

With the Syncfusion PDF Library, you can protect your PDF documents with either of these passwords, or both of them.

If the PDF is encrypted with both types of passwords, you can use either password to open it. But to change the restricted features, you need the permission password.

The complete details to encrypt and decrypt PDF files in C# are explained in the following topics in this post:

Encrypt PDF document

The Syncfusion PDF Library provides support for basic to advanced encryption standards. The following are the encryption standards supported:

  • RC4 40-bit
  • RC4 128-bit
  • AES 128-bit
  • AES 256-bit Revision 5
  • AES 256-bit Revision 6

You can set the algorithm and key size using the properties Algorithm and KeySize available in the class PdfSecurity.

You can follow these three steps to encrypt an existing PDF document using Syncfusion PDF Library:

  1. Load the existing PDF document.
  2. Set a user password, encryption algorithm, and key size.
  3. Save the PDF document.

The following code example shows how to encrypt a PDF document using a user password in C#.

//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");

//Create a document security.
PdfSecurity security = document.Security;

//Set user password for the document.
security.UserPassword = "sample123";
//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size.
security.KeySize = PdfEncryptionKeySize.Key256Bit;

//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);

Execute this code example to get a PDF document like in the following screenshot.Encrypted PDF document

Restrict PDF permissions

You can customize the permission settings to allow or restrict certain actions such as:

  • Copying content
  • Printing
  • Editing the document
  • Filling form fields

The following table lists the permission flags supported by Syncfusion PDF Library.

Permission flagDescription
FullQualityPrintAllows users to print a clear copy of the document.
PrintAllows users to print the document with low quality.
FillFieldsAllows users to fill out and sign a form.
CopyContentAllows users to copy content from the document.
AccessibilityCopyContentEnables content screen reading for users with disabilities.
EditAnnotationsAllows users to get user input or feedback on a document.
AssembleDocumentAllows users to manipulate the pages in the document.
EditContentAllows users to edit the content in the document.

To restrict PDF permissions, you must set the permission password (owner password) for the PDF document. You can set the owner password for the PDF document using the OwnerPassword property available in the class PdfSecurity.

You can set various permissions for the PDF document using the Permissions property available in the class PdfSecurity.

The following code example shows how to encrypt a PDF document using an owner password in C#.

//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");

//Create document security.
PdfSecurity security = document.Security;

//Set owner password for the document.
security.OwnerPassword = "sample123";
//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size.
security.KeySize = PdfEncryptionKeySize.Key256Bit;

//Set various permissions for the PDF document.
security.Permissions = PdfPermissionsFlags.Print |
PdfPermissionsFlags.FillFields |
PdfPermissionsFlags.CopyContent |
PdfPermissionsFlags.FullQualityPrint;

//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);

Execute this code example to get a PDF document like in the following screenshot.Permission restricted PDF document

PDF encryption options

PDF encryption options are used to select the content needing to be encrypted in the resultant PDF document. It has following three options:

Encrypt All Document Contents: Encrypts both the document and its metadata. This makes the document undiscoverable by search engines, as its metadata is encrypted.

Encrypt All Document Contents Except Metadata: Encrypts only the contents of the document, leaving the metadata. This will allow search engines to read the metadata.

Encrypt Only File Attachments: Users can open the document without a password. But they need a password to open its attachments. Use this option to create security envelopes.

You can set these options in PdfSecurity using the property EncryptionOptions.

//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf");

//Create a document security.
PdfSecurity security = document.Security;

//Set owner password.
security.OwnerPassword = "sample123";
//Set user password.
security.UserPassword = "user";

//Set encryption algorithm.
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Set key size
security.KeySize = PdfEncryptionKeySize.Key256Bit;

//Set various permissions for the PDF document.
security.Permissions = PdfPermissionsFlags.Print |
PdfPermissionsFlags.FillFields |
PdfPermissionsFlags.CopyContent |
PdfPermissionsFlags.FullQualityPrint;

//Encrypt all the content except metadata.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;

//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);

Execute this code example to get a PDF document like in the following screenshot.PDF encrypted with the metadata exposed to search engines

Decrypt a PDF document

Follow these steps to decrypt an encrypted PDF file:

  1. Load the PDF document as PdfLoadedDocument using the OwnerPassword (since UserPassword cannot be used to alter the security permissions).
  2. Set the security permission to default.
  3. Set the owner password and user password to empty.
  4. Save the resultant document.

The following code example shows how to decrypt a PDF document in C#.

//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf", "user");

//Create a document security.
PdfSecurity security = document.Security;

//Set permissions to default.
security.Permissions = PdfPermissionsFlags.Default;
//Set owner password.
security.OwnerPassword = String.Empty;
//Set user password.
security.UserPassword = String.Empty;

//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);

By executing this code example, you will get a PDF document like in the following screenshot.Decrypted PDF document

Conclusion

In this blog post, we have walked through how to encrypt and decrypt a PDF document in C# using Syncfusion PDF Library.

So, now you can easily add password security to your PDF document with sensitive information and remove the password of an encrypted document if you want to open access to it to everyone.

Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples.

If you have any questions about these features, please let us know in the comments below. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are happy to assist you!

If you like this article, we think you would also like the following articles about our PDF Library:

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin

Leave a comment

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top