Copied RSS Feed

PDF

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:

Experience a leap in PDF technology with Syncfusion's PDF Library, shaping the future of digital document processing.

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.

Explore the wide array of rich features in Syncfusion's PDF Library through step-by-step instructions and best practices.

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 flag Description
FullQualityPrint Allows users to print a clear copy of the document.
Print 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.

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

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.

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.

GitHub resource

Also, you can find all the examples of encrypt and decrypt PDF files in the GitHub repository.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

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:

Meet the Author

Praveenkumar

Praveenkumar is the Product Manager for PDF at Syncfusion. He has been a .NET developer since 2012. He is specialized in PDF file format and .Net technologies.