How to password protect pdf programmatically in .NET?

情到浓时终转凉″ 提交于 2019-12-14 03:45:37

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/15173389/how-to-password-protect-pdf-programmatically-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!