Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to password protect pdf programmatically in .NET?

I need to programmatically protect a PDF file with a password in C#. The same PDF file must be saved with different names and different password.

Does anyone know a method for this (no expensive tools, please..)?

like image 885
Dave von Orlando Avatar asked Mar 02 '13 10:03

Dave von Orlando


1 Answers

It can be done using itextsharp:

using (var input = new FileStream("test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (var output = new FileStream("test_encrypted.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
    var reader = new PdfReader(input);
    PdfEncryptor.Encrypt(reader, output, true, "userPassword", "userPassword", PdfWriter.ALLOW_PRINTING);
}
like image 184
MarcinJuraszek Avatar answered Oct 12 '22 22:10

MarcinJuraszek