Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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

                    }
                }
Posted
Updated 4-Apr-14 1:20am
v6
Comments
[no name] 4-Apr-14 7:32am    
Once you download file It will be stored in temp folder of local machine. Then it will not be possible to control it. So you can't restrict user for save as. For more security you can restrict copy content, restrict print also but not save as option.
kamal-kondi 5-Apr-14 7:38am    
Thanks for response , Yes i am also agree but the client requirement asking to Restrict save as while downloading the pdf file,I did the restrict copy, edit while downloading the pdf. if u have any solution please update to me its urgent

No you cant, due to security reason, You can not control the client machines. What you can do is to restrict user from download or Save the files. This can be done using Jquery key detect checks.
 
Share this answer
 
Comments
kamal-kondi 5-Apr-14 7:59am    
Hi puneet , i did While downloading can restrict the can't copy ,edit and print buttons ,but why we can't to disable the save as in file menu or menu bar in pdf file .

If u get any solution plz replay me its urgent !!!!!!!!!!!!
Er. Puneet Goel 5-Apr-14 8:07am    
This is due to obscurity of the client as if you were get such control, using any machine on internet will not be safe any more. This is verified by myself.

You can try alternatives like:

1. How about opening your page using window.open from javascript
This option lets you control lots of properties of web-browser, may
be you can control "Save As" option too, never tried it though

2. Also remember to disable the right click option on the page
http://javascript.internet.com/page-...ght-click.html

3. Even if you disable both of the above, you will be unable to stop
user from doing PRNT+Screen :)
Hi Friends I solved ,Hide the menubar in pdf only.

I paste the code here
PdfReader reader = new PdfReader(input);
reader.ViewerPreferences = PdfWriter.HideMenubar


Still i am searching for Save as option in menu bar
 
Share this answer
 
Comments
maha khedr 10-Mar-16 4:59am    
this doesn't work with me I just have the Print option disabled!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900