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:
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:
Install-Package Syncfusion.Pdf.Net.Core
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:
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.
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:
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.
With the help of our .NET PDF Library, you can easily protect a PDF document. To do so, please follow these steps:
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.
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:
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);
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:
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.
Changing the permissions of 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:
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);
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:
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.
To gain a better understanding, you can explore examples for Securing a PDF document using C# on our GitHub repository.
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!