Add a password to a PDF, allowing no one to even view the file unless they have the password.

PDF is used for both business and personal use. It comes with different features and functions, including Graphics, page order, Carpool working, etc. which are quite handy in many fields. However, with such wide adoption Privacy issues also arise, requiring proper protection.

XsPDF lets you encrypt your PDF documents, adding password protection for restricting permissions, or entirely locking the PDF from any modifications or even require a password to view the document.

In this C# .NET tutorial, learn how to protect PDF by using the Protect tool to keep your information safe. Keep others from copying, printing and editing the information in your PDF documents with just a password.


// Open an existing document.
PdfDocument document = PdfReader.Open("file1.pdf");

PdfSecuritySettings securitySettings = document.SecuritySettings;

// Add user password and owner password 
securitySettings.UserPassword  = "user password";
securitySettings.OwnerPassword = "owner password";

// Set passwords security level
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit;

// Restrict permission rights. Disallow all the editing rights.
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = false;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = false;
securitySettings.PermitPrint = false;

// Save the document...
document.Save("Encrypt.pdf");

While the PDF file has a user password, the Adobe Reader will prompt for a password. If you want to show the PDF document directly, you can set an owner password instead of a user password. There will be no prompt when Adobe Reader opens the file, but editing will be prevented.

More PDF tutorial