DEV Community

Cover image for Secure Your PDF Documents Like a Pro Using C#
Jollen Moyani for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

Secure Your PDF Documents Like a Pro Using C#

The Syncfusion .NET PDF Library offers a comprehensive solution for securing PDF documents and ensuring the confidentiality and integrity of sensitive information. Users can leverage the PDF Library to implement various security measures and safeguard their PDF files.

The framework also empowers users to apply permissions and restrictions to PDF documents, granting precise control over various actions, including printing, copying, modifying, and content extraction. This capability lets document owners control their PDF files, preventing unauthorized distribution or modification.

Users can encrypt their PDF documents using the following encryption algorithms:

  • Rivest Cipher 4 (RC4)
  • Advanced Encryption Standard (AES)

In this article, we will delve into the capabilities of the Syncfusion .NET PDF Library to secure PDF documents. We will cover the following topics:

Getting started with app creation

1.First, create a .NET console application using Visual Studio.

2.Go to Tools -> NuGet package Manager -> Package Manager Console and open the Package Manager Console.

3.Next, execute the following command to install the Syncfusion.Pdf.Net.Core NuGet package.

Install-Package Syncfusion.Pdf.Net.Core
Enter fullscreen mode Exit fullscreen mode

Encrypt a PDF document with a user password

By setting a user password for a PDF document, you can prevent unauthorized individuals from opening or viewing it. When attempting to open the PDF document, Adobe Acrobat or Reader will prompt the user to enter the user password.

If the user password entered is incorrect, the document will remain inaccessible. This way, you can effectively secure your PDF documents.

Follow these steps to encrypt PDF documents with a user password:

1.Create a new PDF document using the PdfDocument class.
2.Then, add a new blank page using the PdfPage class.
3.Create an instance of the PdfSecurity class.
4.Specify the Algorithm property as RC4 through the PdfEncryptionAlgorithm enum and the KeySize property as 40bit or 128bit through the PdfEncryptionKeySize enum in the PdfSecurity class.
5.Set the UserPassword property of the PdfSecurity class.
6.Add some text using the DrawString method of the PdfGraphics class.
7.Finally, save the PDF document to the memory stream.

Refer to the following code example to encrypt the PDF document with a UserPassword.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create instance of document security.
PdfSecurity security = document.Security;
//Specifies key size, encryption algorithm and user password.
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.Algorithm = PdfEncryptionAlgorithm.RC4;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypt PDF with user password and RC4 128bit keysize", new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, our PDF document will be protected with a user password, showing the security settings as follows.

Encrypting a PDF document with a user password

Encrypting a PDF document with a user password

Encrypt a PDF document with an owner password

By setting an owner password, you can establish various PDF document restrictions, such as printing, content copying, editing, page extracting, and commenting. Acrobat will then prompt for this password whenever any changes need to be made to the PDF. Applying a PDF owner password provides an additional layer of security, further protecting the integrity of the PDF document.

Follow these steps to encrypt a PDF document with an owner password:

1.Initialize a new instance of the PdfDocument class.
2.Add a new blank page using the PdfPage class.
3.Create an instance of the PdfSecurityclass.
4.Specify the Algorithm property as AES through the PdfEncryptionAlgorithm enum and the KeySize property as 40bit, 128bit, or 256bit through the PdfEncryptionKeySize enum in the PdfSecurity class.
5.Set the OwnerPassword property of the PdfSecurity class.
6.Add some text using the DrawString method of the PdfGraphics class.
7.Save the PDF document to the memory stream.

Refer to the following code example to encrypt the PDF document with an OwnerPassword.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create instance of document security.
PdfSecurity security = document.Security;
//Specifies key size, encryption algorithm and user password.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.OwnerPassword = "Syncfusion";
//Draw the text.
graphics.DrawString("This document protected with owner password", new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing the previous code example, our PDF document will be protected with an owner password, and it will show the security settings as follows.

Encrypting a PDF document with an owner password

Encrypting a PDF document with an owner password

Protect a PDF document

With the help of our .NET PDF Library, you can easily protect a PDF document. To do so, please follow these steps:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Create an instance of the PdfSecurity class.
3.Specify the Algorithm property as AES through the PdfEncryptionAlgorithm enum and the KeySize property as 40bit, 128bit, or 256bit through the PdfEncryptionKeySize enum in the PdfSecurity class.
4.Set the owner and user passwords using the UserPassword and OwnerPassword properties of the PdfSecurity class.
5.Save the PDF document to the memory stream.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//PDF document security.
PdfSecurity security = document.Security;
//Specifies encryption key size, algorithm, and permission. 
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Provide owner and user password.
security.OwnerPassword = "ownerPassword256";
security.UserPassword = "userPassword256";

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

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

Protecting a PDF document

Protecting a PDF document

Change the password of a PDF document

The Syncfusion .NET PDF Library allows you to change both the user and owner passwords of a PDF document. This lets you enhance the existing password strength or grant access to authorized individuals.

Follow these steps to change the password of a PDF document:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Change the password of the PDF document using the UserPassword and OwnerPassword properties of the PdfSecurity class.
3.Save the PDF document into the memory stream.

Refer to the following code example.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "ownerPassword256");
//Change the user password.
loadedDocument.Security.UserPassword = "NewUserPassword";
//Change the owner password.
loadedDocument.Security.OwnerPassword = "NewOwnerPassword";

//Save the document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
Enter fullscreen mode Exit fullscreen mode

Change the permissions of a PDF document

You can modify permissions for a PDF document using the .NET PDF Library. This will provide you complete control over access, editing, printing, and other operations performed on the file.

Follow these steps to change the permissions of a PDF document:

1.Use the PdfLoadedDocument class to load an existing PDF document.

2.Change the permission settings of the PDF document using the Permissions property of PdfSecurity class.
3.Save the modified document into the memory stream.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "ownerPassword256");
//Change the permission.
loadedDocument.Security.Permissions = PdfPermissionsFlags.CopyContent | PdfPermissionsFlags.AssembleDocument;

//Save the document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the PDF document.
loadedDocument.Close(true);
Enter fullscreen mode Exit fullscreen mode

Refer to the following images.

Document Security Input PDF file Document Security Output PDF file

Changing the permissions of a PDF document

Protect attachments in a PDF document

We can also encrypt attachments in a PDF document with strong passwords. This adds authenticity to the PDF.

Follow these steps to protect attachments in a PDF document:

1.Use the PdfLoadedDocument class to load a PDF document.

2.Create an instance of the PdfSecurityclass.
3.Specify the Algorithm property as AES through the PdfEncryptionAlgorithm enum and the KeySize property as 40bit, 128bit, or 256bit through the PdfEncryptionKeySize enum in the PdfSecurity class.
4.Set the user password using the UserPassword property of the PdfSecurity class.
5.Specify the EncryptionOptions property as EncryptOnlyAttachments through the PdfEncryptionOptions enum in the PdfSecurity class. This will encrypt only the attachment files in a PDF document.
6.Save the PDF document to the memory stream.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//PDF document security.
PdfSecurity security = document.Security;
//Specifies encryption key size, algorithm and permission.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Provide user password.
security.UserPassword = "password";
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
Enter fullscreen mode Exit fullscreen mode

Remove a password from a PDF document

The .NET PDF Library also enables you to remove passwords from PDF documents. This lets you regain unrestricted access to the content, facilitating seamless editing, printing, and other operations without the need for password authentication.

Follow these steps to remove a password from a PDF document:

1.Use the PdfLoadedDocument class to load an encrypted PDF document.

2.Remove the UserPassword from the PDF document by setting it to an empty string.
3.Save the modified document into the memory stream.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "password");
//Change the user password.
loadedDocument.Security.UserPassword= string.Empty;

//Save the document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
Enter fullscreen mode Exit fullscreen mode

By executing this code example, you can remove the password protection from a PDF document. The resulting PDF document will not have password protection and can be freely opened, edited, and printed.

Removing a password from a PDF document

Removing a password from a PDF document

GitHub reference

To gain a better understanding, you can explore examples for Securing a PDF document using C# on our GitHub repository.

Conclusion

Thanks for reading! In this blog, we’ve seen the various security protection options available in our Syncfusion .NET PDF Library using C#. With these features, you can enhance the confidentiality, integrity, and authenticity of your PDF documents, ensuring they remain secure throughout their lifecycle.

Take a moment to look at the documentation, where you will find other options and features, all with accompanying code samples.

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

Related blogs

Top comments (0)