C# tutorial: encrypt a PDF file


Encrypt a PDF file

When you create a PDF document, you might want to keep it secured so that users that don't have permissions are not able to view the document, copy, or fill in forms. The PDF document can be protected by two passwords: a user password and an owner password. If the PDF is protected with a user password, you'll have to enter this password before you can open the document in Adobe Reader. The owner password applies permission settings to the PDF document. For example, you can disallow printing, copying, document, or filling in forms in the PDF document.

iTextSharp allows you to implement security feature to your PDF document by using the SetEncryption method of PdfWriter class. Below is the general form of the SetEncryption method is shown below:

setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) 

- userPassword--an array of bytes representing the user password.

- ownerPassword--an array of bytes representing the owner password.

- permissions--permission settings that will be applied to the created document. These settings can be AllowPrinting, AllowCopy, AllowFillIn, AllowScreenReaders, AllowAssembly, or AllowDegradedPrinting. 

- encryptionType--the type of encryption. It can be one of the following STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128



Example:

var doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("d:/pdfdoc.pdf", FileMode.Create));
byte[] USER=Encoding.ASCII.GetBytes("userpwd");
byte[] OWNER=Encoding.ASCII.GetBytes("ownerpwd");
writer.SetEncryption(USER, OWNER, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128);

pdf encryption

 

Encrypting and decrypting an existing PDF file

Encrypting an existing PDF file is simple in iTextSharp. You can accomplish the task with the PdfStamper.

byte[] USER = Encoding.ASCII.GetBytes("userpwd");
byte[] OWNER = Encoding.ASCII.GetBytes("ownerpwd");
PdfReader reader = new PdfReader("D:/oop-software-development.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream("D:/encryptedpdfdoc.pdf",FileMode.Create));
stamper.SetEncryption(USER, OWNER, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128);
stamper.Close();

To decrypt the PDF file, you need to provide the correct owner password when a PdfReader object is created to read from that file.

byte[] OWNER = Encoding.ASCII.GetBytes("ownerpwd");
PdfReader reader = new PdfReader("D:/encryptedpdfdoc.pdf",OWNER);
PdfStamper stamper = new PdfStamper(reader, new FileStream("D:/decryptedpdfdoc.pdf", FileMode.Create));
stamper.Close();


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.