0

I am using C# Web Application to print reports using Rdlc report(version 15.0.0.0) in .NET framework 4.5.2 . On Printing reports continuously , I often encounter this problem where the report doesn't load in PDFViewer and PDFViewer preview appears blank/stuck.

Please let me know proper workaround if any available to combat the issue.

PFA the code snippet am using to print reports and an image illustrating the issue

  1. main class (code behind cs)
 public void printreport(long JobId)
        {
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            string devinfo = "<DeviceInfo><ColorDepth>32</ColorDepth><DpiX>350</DpiX><DpiY>350</DpiY><OutputFormat>PDF</OutputFormat>" +
                   "  <PageWidth>8.5in</PageWidth>" +
                    "  <PageHeight>11in</PageHeight>" +
                    "  <MarginTop>0in</MarginTop>" +
                    "  <MarginLeft>0.2in</MarginLeft>" +
                     "  <MarginRight>0.2in</MarginRight>" +
                     "  <MarginBottom>0in</MarginBottom>" +
                   "</DeviceInfo>";

            try
            {
                //----Filling the datatable with contents from SQL Table------

                string CommandText = "Select * from Jobs";
                SqlCommand cmdAzure = new SqlCommand(CommandText, connectAzure);                                                 
                SqlDataAdapter adepazure = new SqlDataAdapter(cmdAzure);
                DataTable dt = new DataTable();                          
                adepazure.Fill(dt);              

                //---Creating rdlc report definition and adding datatable to the rdlc datasource
                
                ReportViewer viewer = new ReportViewer();
                viewer.LocalReport.DataSources.Clear();
                ReportDataSource rds = new ReportDataSource("Jobs", dt);               
                viewer.LocalReport.ReportPath = @"bin\JobsCoverPage.rdlc";
                viewer.LocalReport.DataSources.Add(rds);

                //---Converting the report into byte stream
                
                byte[] bytes = viewer.LocalReport.Render("PDF", devinfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
                // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
                string path = Server.MapPath("");
                Random rnd = new Random();
                int month = rnd.Next(1, 13); // creates a number between 1 and 12
                int dice = rnd.Next(1, 7); // creates a number between 1 and 6
                int card = rnd.Next(9); // creates a number between 0 and 51
                string file_name = "Jobs" + "Reports" + month + dice + card + ".pdf"; //save the file in unique name 
                variable = file_name;
                Session["value"] = file_name;

                //---After that use file stream to write from bytes to pdf file on your server path

                FileStream file = new FileStream(path + "/" + file_name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                file.Write(bytes, 0, bytes.Length);
                file.Dispose();

                Page.ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:child_open(); ", true);
            }
            catch (Exception ex)
            {

            }

        }
  1. Javascript to open pdf file in pop up
 <script type="text/javascript">
    
     function child_open() {
         var upfile = '<%= this.variable %>'; 
         popupWindow = window.open('../Print.aspx?dfile=' + upfile, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=850,height=700');
     }
         
 </script>
  1. Print.aspx.cs
 protected void Page_Load(object sender, EventArgs e)
        {
            string file_name, path=null;
            if(Request.QueryString["dfile"] != null) 
            {
                 file_name = Request.QueryString["dfile"];
                 path = Server.MapPath("/WebForm/" + file_name);
            }

            // Open PDF File in Web Browser 

            System.Net.WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(path);
            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }
        } 

0 Answers0