Secure Your PDF Documents Like a Pro Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Securing PDF documents in C# with the Syncfusion .NET PDF library

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:

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

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

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);

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

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

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 PdfSecurity class.
  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);

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);

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

Protecting a PDF document
Protecting a PDF document

Witness the advanced capabilities of Syncfusion's PDF Library with feature showcases.

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);

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);

Refer to the following images.

Document Security Input PDF fileDocument 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 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 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);

See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.

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);

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.

Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.

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

Tags:

Share this post:

Comments (1)

I am using the following code, which create a document from a template merge. However when you open it, it says, “There is an error on this page. Acrobat may not display the page correctly Please contact…” Note: This does not happen when I do not apply the encryption, so the merge and everything else works perfectly. I also notice that if I accept the message, then click on the ‘lock’ icon, that it displays perfectly.
document.MailMerge.Execute(fieldNames, fieldValues);
DocIORenderer render = new DocIORenderer();
Syncfusion.Pdf.PdfDocument pdfDocument = render.ConvertToPDF(document);
pdfDocument.Compression = Syncfusion.Pdf.PdfCompressionLevel.Best;
PdfSecurity security = pdfDocument.Security;
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.OwnerPassword = “user”;
//convert to stream and store in blob storage
render.Dispose();
document.Dispose();
MemoryStream stream = new();
stream.Position = 0;
pdfDocument.Save(stream);
pdfDocument.Close();
stream.Position = 0;
await _azureBlobStorage.StoreStreamAsync(stream, $”Certificate{captureId}.pdf”, “certificates”);
stream.Close();

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed