Password Protected PDF ,ASP.NET C#

Posted by Raja_89 under C# on 10/11/2013 | Points: 10 | Views : 57174 | Status : [Member] | Replies : 2
Hai
Is it possible to create a pdf from database which must be password protected and encrypted


Kindly explain with code

Regards




Responses

Posted by: Samirbhogayta on: 10/15/2013 [Member] Starter | Points: 25

Up
0
Down
Yes, there are two passwords that you can pass to PdfEncryptor.Encrypt(), userPassword and ownerPassword. Just pass null to the userPassword and people will be able to open it without specify a password.

string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "Test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "Test_enc.pdf");

using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, null, "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}

SAMIR
Sr. Software Engineer

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramumohan on: 3/10/2016 [Member] Starter | Points: 25

Up
0
Down
HI Samir,

I have tried with the below code to open the protected PDF file .But i could not able to complete the task as i am getting error Bad user Password please if possible can u please suggest me any solution to complete the task

Thanks in advance

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response