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:
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:
The Syncfusion PDF Library provides support for basic to advanced encryption standards. The following are the encryption standards supported:
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:
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.
You can customize the permission settings to allow or restrict certain actions such as:
The following table lists the permission flags supported by Syncfusion PDF Library.
Permission flag | Description |
FullQualityPrint | Allows users to print a clear copy of the document. |
Allows users to print the document with low quality. | |
FillFields | Allows users to fill out and sign a form. |
CopyContent | Allows users to copy content from the document. |
AccessibilityCopyContent | Enables content screen reading for users with disabilities. |
EditAnnotations | Allows users to get user input or feedback on a document. |
AssembleDocument | Allows users to manipulate the pages in the document. |
EditContent | Allows 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.
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.
Follow these steps to decrypt an encrypted PDF file:
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.
Also, you can find all the examples of encrypt and decrypt PDF files in the GitHub repository.
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: