Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the height of a SSRS report in C#?

Hi I am working on a MVC Project in which I have a reports page where user can view reports with the use of a report viewer.

I need to set the page size dynamically for a report, I tried many ways to solve this but I couldn’t.

I can change the ReportViewer Size with this code

rptviewer.Height = Unit.Pixel(520);

Kindly help me in with the following queries.

1.Is it possible to change the SSRS report page height using C# Code?

2.Is it possible to change the paper size in the runtime?

My Previous workarounds

<-------- 1 --------->

 System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
                pg.Margins.Top = 0;
                pg.Margins.Bottom = 0;
                pg.Margins.Left = 0;
                pg.Margins.Right = 0;
                System.Drawing.Printing.PaperSize size = new PaperSize(); 
                size.RawKind = (int)PaperKind.A5;
                pg.PaperSize = size;  
                rptviewer.SetPageSettings(pg);  
                ViewBag.ReportViewer = rptviewer;
                return View("_ReportView");

<-------- 2 --------->

 System.Drawing.Printing.PageSettings MyPageSize= new System.Drawing.Printing.PageSettings(); 
 MyPageSize.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 17, 12); 
 rptviewer.SetPageSettings(MyPageSize);

<-------- 3 --------->

var setup = rptviewer.GetPageSettings();           
setup.PaperSize.Height = 1500;
rptviewer.SetPageSettings(setup);

None of the above logic worked for me :-(

like image 665
Gokul Maha Avatar asked Nov 08 '22 09:11

Gokul Maha


1 Answers

The thing is, In order to control the page size in the rendering process we need to pass the proper Device Information Settings to the report at run time. This will work for physical page oriented renders like PDF, Image, etc. This is a simple Xml string that can be passed as a parameter to the report in order to control these settings. Each export type has a different set of properties that can be overridden and controlled in this way.

In the following example, an export to PDF is performed and passes the page height and width to match an A4 paper size as the targeted device (line 66):

 private void RenderReportToClient()
 {
     //set credentials
     RSExecuteProxy.ReportExecutionService rs = new RSExecuteProxy.ReportExecutionService();
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

    RSProxy.ReportingService2005 rsInfo = new RSProxy.ReportingService2005();
    rsInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;
     // init render args
    byte[] result = null;
    string reportPath = rptViewer.ServerReport.ReportPath;
   string format = "PDF";
    string historyId = null;
     string encoding;
    string mimeType;
     string extension;
    RSExecuteProxy.Warning[] warnings = null;
     string[] streamIDs = null;
     //init exec info
     RSExecuteProxy.ExecutionInfo execInfo = new RSExecuteProxy.ExecutionInfo();
     RSExecuteProxy.ExecutionHeader execHeader = new RSExecuteProxy.ExecutionHeader();
     rs.ExecutionHeaderValue = execHeader;
     //get report
     execInfo = rs.LoadReport(reportPath, historyId);
     String SessionId = rs.ExecutionHeaderValue.ExecutionID;
    //get parameter info
     ReportParameterInfoCollection parameters = rptViewer.ServerReport.GetParameters();
    //figure out how many parameters we will have 
     //those with multi-value will need there own ParameterValue in the array
    int paramCount = 0;
     foreach (ReportParameterInfo pramInfo in parameters)
     {
         paramCount += pramInfo.Values.Count;
    }

    RSExecuteProxy.ParameterValue[] prams = new SSRSWeb.RSExecuteProxy.ParameterValue[paramCount];
    int currentPramPosition = 0;

     //set pram values
     foreach (ReportParameterInfo pramInfo in parameters)
     {
         foreach (string pramValue in pramInfo.Values)
         {
           prams[currentPramPosition] = new SSRSWeb.RSExecuteProxy.ParameterValue();
            prams[currentPramPosition].Label = pramInfo.Name;
            prams[currentPramPosition].Name = pramInfo.Name;
         prams[currentPramPosition].Value = pramValue;
            currentPramPosition++;
        }
      }

       rs.SetExecutionParameters(prams, "en-US");

      //build the device settings  (A4 8.3 × 11.7)
       string deviceInfo = string.Format("<DeviceInfo><PageHeight>{0}</PageHeight><PageWidth>{1}</PageWidth></DeviceInfo>", "11.7in", "8.3in");

    //get report bytes
     result = rs.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

       Response.ClearContent();
      Response.AppendHeader("Content-Disposition", "inline;filename=report.pdf");
       Response.AppendHeader("content-length", result.Length.ToString());
     Response.ContentType = "application/pdf";
    Response.BinaryWrite(result);
    Response.Flush();
     Response.Close();
   }

When the report is saved you can view the PDF and examine the properties and notice that the page height and width is 8.3in x 11.7in as specified.

like image 54
Barr J Avatar answered Nov 14 '22 05:11

Barr J