.NET PDF Examples
Protect PDFs in C# with the .NET PDF Library
The Syncfusion® .NET PDF Library offers powerful capabilities for creating, reading, and editing PDF documents. Users can protect PDF files with encryption and permissions for document security, restricted access, and prevention of unauthorized modifications.
Watch this video to see how to protect PDF files using the Syncfusion .NET PDF Library.
Password protect and encrypt PDF documents in C#
Learn how to programmatically encrypt PDF documents and add password protection in C# using the Syncfusion .NET PDF Library. This guide demonstrates PDF security with AES encryption and user permissions.
Step 1: Create a new C# Console Application project
Begin by creating a new C# Console Application project in Visual Studio or your preferred IDE to implement PDF security functionality.
Step 2: Install Syncfusion PDF NuGet package
Install the Syncfusion.Pdf.Net.Core NuGet package in your C# project from NuGet.org. This package provides PDF encryption and security APIs.
Step 3: Add required namespaces for PDF protection
Import the following namespaces in your Program.cs file to access PDF security classes and encryption methods:
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;Step 4: Load the PDF document to protect
Use the PdfLoadedDocument class to load your existing PDF file that you want to encrypt and password protect.
//Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
{
}Step 5: Configure PDF security
Get the PdfSecurity object from the loaded document. Set the encryption algorithm to AES (Advanced Encryption Standard) and specify a 256-bit key size for maximum security protection.
// Get the security settings of the document
PdfSecurity security = loadedDocument.Security;
// Set encryption to AES with a 256-bit key
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;Step 6: Set owner and user passwords
Configure both owner and user passwords. The owner password allows full access to modify security settings, while the user password restricts document access.
// Set owner and user passwords for the document
security.OwnerPassword = "owner123";
security.UserPassword = "user123";Step 7: Save the password-protected PDF document
Save the encrypted PDF document using the Save method. The output file will be protected with AES encryption and require passwords to access.
// Save the secured PDF document
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));GitHub project
NuGet installation
Get started quickly by downloading the installer and checking license information on the Downloads page.
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Learning
Technical Support
