Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Hi All,
Please help me,

when generating the report with pdf by report viewer how can i set password to pdf.

i did by using code behind with type pdf,but i want when export to pdf clicking that time i need to set password for generated pdf file.

what i did i paste the code here.

C#
using Microsoft.Reporting.WebForms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Text;

public void DownloadCertificate()
{
 rvLMSReport.LocalReport.DataSources.Add(new ReportDataSource("CertificateofTraining", TraineeCertificat));
                rvLMSReport.LocalReport.EnableExternalImages = true;
                string reportType = "PDF";
                string mimeType;
                string encoding;
                string fileNameExtension;
                string deviceInfo =
                "<deviceinfo>" +
                "  <outputformat>PDF</outputformat>" +
                "  <pagewidth>21cm</pagewidth>" +
                "  <pageheight>29.7cm</pageheight>" +
                "</deviceinfo>";
                Warning[] warnings;
                string[] streams;
                byte[] renderedBytes;
                //Render the report
                renderedBytes = rvLMSReport.LocalReport.Render(
                    reportType,
                    deviceInfo,
                    out mimeType,
                    out encoding,
                    out fileNameExtension,
                    out streams,
                    out warnings);
                //Response.Clear();
                //Response.ContentType = mimeType;
                //string path = System.IO.Path.GetTempPath();
                //filePath = System.IO.Path.Combine(path, reportname + "." + fileNameExtension);
                //Response.AddHeader("content-disposition", "attachment; filename=" + reportname + "." + fileNameExtension);
                //Response.BinaryWrite(renderedBytes);
                // Response.End();
                ReportEntityOperations objreportentity = new ReportEntityOperations();
                objreportentity = TraineeCertificat.ToList().FirstOrDefault();
                string CertificatePassword = GetUniqCode(8);
                string Username = objreportentity.UserFullName;
                string Coursename = objreportentity.CourseName;
                string mailSub = "Your Status";
                CompanyEmailSetting ObjComEmailSetting = new CompanyEmailSetting();
                CompanyEmailSettingBLL objCompanyEmailSettingBLL = new CompanyEmailSettingBLL();
                LanguageCompanyEmailSetting LangESettings = new LanguageCompanyEmailSetting();
                LanguageCompanyEmailSettingBLL objLanguageCompanyEmailSettingBLL = new LanguageCompanyEmailSettingBLL();
                ObjComEmailSetting = objCompanyEmailSettingBLL.SelectByCompanyId(objreportentity.CompanyId);
                string mailBody = "You have get the Certificate Password " + "'" + CertificatePassword + "'" + " and with this you can open the Your Certificate'" + objreportentity.CourseName + "'Cresent'";
                SendMail(objreportentity.TrainingRequestId, Guid.Empty, "Hello "+objreportentity.UserFullName, "Course", ObjComEmailSetting.EmailSMTPAddress, ObjComEmailSetting.EmailFromAddress, mailSub, mailBody, "kamalakar.kondi@bizbites.com", objreportentity.CompanyId, 1);
                using (MemoryStream input = new MemoryStream(renderedBytes))
                {
                    using (MemoryStream output = new MemoryStream())
                    {
                        PdfReader reader = new PdfReader(input);
                        PdfEncryptor.Encrypt(reader, output, true, CertificatePassword, CertificatePassword, PdfWriter.ALLOW_SCREENREADERS);
                        renderedBytes = output.ToArray();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "attachment;filename=" + reportname + "." + fileNameExtension);
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.BinaryWrite(renderedBytes);
                        Response.Flush();
                        Response.Close();
                        //Response.End();
                    }
                }

            }
            catch (Exception ex)
            {

            }
Posted
Updated 1-Apr-14 2:17am
v6

1 solution

 
Share this answer
 
Comments
kamal-kondi 4-Apr-14 7:09am    
thanks Rakesh

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