Below is my Code I am generating Password Protected pdf from ItextSharp.
Actually two pdf are getting generating and saving.
But i want only file to be saved.
If I use same for input and output i am getting error.
Truly appreciate your help.
Letter1 mydoc = new Letter1();
mydoc.GenerateLetter();
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string InputFile = Path.Combine(WorkingFolder, "Testing1.pdf");
FileStream f = new FileStream(InputFile, FileMode.Create);
f.Write(mydoc.DocumentBytes, 0, mydoc.DocumentBytes.Length);
f.Close();
string OutputFile = Path.Combine(WorkingFolder, "TestingOut1.pdf");
using (Stream input = new FileStream(InputFile, FileMode.OpenOrCreate, 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, "abc123", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
No need to instantiate a separate stream to read the PDF you want to encrypt. Use the PdfReader overloaded constructor that accepts a file path. Something like this: