Tuesday, May 30, 2017

Export rdlc file into PDF with Images

Export rdlc file into PDF with Images

private void CreatePDF(int ApplicantId)
    {

        //MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere();
        Applicant objApp = new Applicant();
        DataSet dsApplicant = objApp.GetApplicantByID(ApplicantId);
        // Create Report DataSource
        ReportDataSource rds = new ReportDataSource("DataSet1", dsApplicant.Tables[0]);

        // Variables
        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;

        // Setup the report viewer object and get the array of bytes
        ReportViewer viewer = new ReportViewer();
        viewer.ProcessingMode = ProcessingMode.Local;
        viewer.LocalReport.ReportPath = Server.MapPath("") + @"\Report\EmployeeW4.rdlc";

        viewer.LocalReport.EnableExternalImages = true;

        string imagePath = new Uri(Server.MapPath("") + @"\Temp\" + dsApplicant.Tables[0].Rows[0]["SignatureImage"].ToString() + ".png").AbsoluteUri;
        ReportParameter parameter = new ReportParameter("Path", imagePath);
        viewer.LocalReport.SetParameters(parameter);
        viewer.LocalReport.Refresh();

        viewer.LocalReport.DataSources.Add(rds); // Add datasource here

        byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
        string fileName = "";
        if (dsApplicant.Tables[0].Rows.Count > 0)
            fileName = dsApplicant.Tables[0].Rows[0]["FirstName"].ToString() + "_" + ApplicantId;
        //Now that you have all the bytes representing the PDF report, buffer it and send it to the client.

        string path = Server.MapPath("") + (@"\W4Form\" + DateTime.Now.ToString("yyyy-MMM-dd"));
        if (!Directory.Exists(path))
        {
            DirectoryInfo di = Directory.CreateDirectory(path);

        }
        using (FileStream stream = new FileStream((path + "/" + fileName + ".pdf"), FileMode.Create))
        {
            stream.Write(bytes, 0, bytes.Length);
        }
    }



Download folder file into Zip 

 string path = Server.MapPath("") + @"\W4Form\" + DateTime.Now.ToString("yyyy-MMM-dd") + @"\";//Location for inside Test Folder
        string[] Filenames = Directory.GetFiles(path);
        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(Filenames, "Project");//Zip file inside filename
            //zip.Save(@"D:\Projectzip.zip");//location and name for creating zip file
            Response.Clear();
            Response.BufferOutput = false;
            string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
            zip.Save(Response.OutputStream);
            Response.End();
        }
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('File has been download successfully.')", true);

No comments:

Post a Comment