Dave von Orlando
Dave von Orlando

Reputation: 81

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..)?

Upvotes: 6

Views: 7193

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

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);
}

Upvotes: 10

Related Questions