I Written Code please check i did all scenarios not getting solution.
Thanks in Advace
using (MemoryStream input = new MemoryStream(renderedBytes))
{
using (MemoryStream output = new MemoryStream())
{
PdfReader reader = new PdfReader(input);
reader.AddViewerPreference(PdfName.HIDEMENUBAR, PdfName.NONE);
// PdfEncryptor.Encrypt(reader, output, true, CertificatePassword, CertificatePassword, PdfWriter.ALLOW_SCREENREADERS);
PdfEncryptor.Encrypt(reader, output, true, null, null, PdfWriter.ALLOW_PRINTING | PdfWriter.AllowPrinting | PdfWriter.ENCRYPTION_AES_128 | PdfWriter.CenterWindow);
renderedBytes = output.ToArray();
Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition", "inline;filename=" + reportname + "." + fileNameExtension + "#menubar=0;");
//Response.AddHeader("content-disposition", "<embed src=" + reportname + "." + fileNameExtension + "'#toolbar=0&scrollbar=0&navpanes=0'");
Response.AddHeader("content-disposition", "attachment;filename=" + reportname + "." + fileNameExtension);
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(renderedBytes);
Response.Flush();
Response.Close();
//string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//string InputFile = Path.Combine(WorkingFolder, "StickerSheet_New.pdf");
//string OutputFile = Path.Combine(WorkingFolder, "2.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, "23!54", "23!54", PdfWriter.ALLOW_SCREENREADERS);
// System.Text.UTF8Encoding Encoding = new System.Text.UTF8Encoding();
// PdfStamper stamper = new PdfStamper(reader, output);
// stamper.SetEncryption(Encoding.GetBytes("23!54"), null, PdfWriter.AllowScreenReaders, PdfWriter.STRENGTH40BITS );
// //output.ViewerPreferences.HideToolbar = true;
// //PdfReader pReader = new PdfReader();
// //PdfEncryptor.Encrypt(reader, new FileStream("1.pdf", FileMode.Open), PdfWriter.STRENGTH128BITS, "23!54", null, PdfWriter.AllowScreenReaders);
// stamper.Close();
// reader.Close();
// Response.Write("PDF created");
// }
// }
// }
//}
}
}
here this same code for more clarity
using (MemoryStream input = new MemoryStream(renderedBytes))
{
using (MemoryStream output = new MemoryStream())
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, null, null, PdfWriter.ALLOW_PRINTING | PdfWriter.AllowPrinting | PdfWriter.ENCRYPTION_AES_128 | PdfWriter.CenterWindow|PdfWriter.HideMenubar);
renderedBytes = output.ToArray();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + reportname + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.Flush();
Response.Close();
}
}