PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

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 Developer Tools
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.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

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

XtremeDocumentStudio .NET

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

PDFOne (for Java™)

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

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

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

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).