PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Encrypting and Decrypting PDF Documents Using PDFOne .NET

Learn about securing and restricting the use of a PDF document. Get the C# code for password-protecting a PDF
By Raju Sinha

An encrypted PDF document places certain restrictions on how the document can be used - to open it, to modify it, to print it, to copy content, etc... An encrypted document may require either one of two passwords - owner or user password. If you provide the owner password, you have full access to the document including the ability to specify new password and change document restrictions. If you provide the user password, you will be able to open the document but may be subject to restrictions placed by the owner of the document. If a document creator encrypted a document without specifying a user password, then anyone can open the document without providing a password althought document restrictions would still apply.

This month's tip is about how to encrypt a document and place restrictions on its users.

To encrypt a document, you have to set PDFDocument.Security.Enabled property to true. (The PDFDocument.Security internally uses an object of the PDFSecurity class.) Here is the C# code to encrypt a PDF:

// Create a new document object
PDFDocument doc = new PDFDocument();

// Load an existing PDF
doc.Load("unencrypted_doc.pdf");

// Enable document encryption
doc.Security.Enabled = true;
doc.Security.Level = PDFEncryptionLevel.AES_128bit;

// Set passwords
doc.Security.UserPassword = "abc";
doc.Security.OwnerPassword = "xyz";  

// Specify document restrictions
doc.Security.UserPermissions = 
    PDFUserPermissions.Commenting | 
    PDFUserPermissions.DocAssembly | 
    PDFUserPermissions.HighResPrint;

// Write encrypted document to file stream
doc.Save("encrypted_doc.pdf");

// Dispose the document object
doc.Close()

In the above code, you will note that the PDFSecurity.UserPermissions property can take a combination of values from the PDFUserPermissions enumeration.

To decrypt an encrypted document, the password is supplied by an event handler registered with the PDFDocument.Password event. This event occurs when you call PDFDocument.Load() method with an encrypted document. Here is the C# code to decrypt a PDF:

// Create a new PDFDocument object
PDFDocument doc = new PDFDocument();

// Specify Password event handler
doc.Password += new PasswordEventHandler(password);

// Load encrypted document
doc.Load("encrypted_doc.pdf"); // Calls Password event handler

// Disable document encryption
doc.Security.Enabled = false;

// Write unencrypted document to file stream
doc.Save("Decryption.pdf");

// Dispose the document object
doc.Close();

The event handler can be implemented as below:

public void password(object d, ref string str, ref bool b) {
 // Supply owner password to decrypt document
 str = "xyz";   // "xyz" is the owner password
}		

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2025 Gnostice Information Technologies Private Limited. All rights reserved.