5

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 :-(

Gokul Maha
  • 298
  • 8
  • 24
  • Your MVC front end application cannot affect how the report is rendered on the report server. The report viewer object is a container to hold the rendered report. Changing its size would not affect the attributes of the report inside it. Can you give us more info about the problem you are trying to solve? – Wes H Mar 16 '18 at 13:45
  • Sure, First thing I missed to tell is I am not using report server, I am using LocalReport to render on to my MVC Application. – Gokul Maha Mar 16 '18 at 13:48
  • I assume you mean that SSRS is installed on the system that hosts the web page. Regardless of which system is acting as the server, if SSRS is serving up the report, you cannot change how the report is rendered externally to the SSRS services. – Wes H Mar 16 '18 at 13:54
  • Objective of the problem is, We have a large report with more than 40 columns and the height of the report is fixed to 11 Inches (Assigned in Report Properties while designing the report). When we load the report into the reportviewer (height 500px) we are able to see both horizontal and vertical scrollbars which is a usual thing.We planned to make this report more responsive,for this height of the report will be calculated with the available empty space & we can assign the calculated height to the report so that we can avoid the vertical scrollbar and it looks good in all the screen size – Gokul Maha Mar 16 '18 at 13:57
  • 1
    The only possible way would be to pass in the height (in inches) as a parameter to the report. Within the report you could right some embedded code to change the page size at run time. However, I do not know if that will work since those attributes are not expression enabled. But you definitely would have to pass the parameter into the report to even be remotely possible. Your MVC report viewer control is just a container and will not be able to affect the definition of the report. – Wes H Mar 16 '18 at 14:04
  • Can you give the registry a go and let me know if this solves it for you? https://stackoverflow.com/questions/46836345/how-to-set-paper-size-and-margins-printing-from-a-web-browser-control/47028484 – Jeremy Thompson Nov 13 '18 at 00:55

1 Answers1

1

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.

Barr J
  • 10,636
  • 1
  • 28
  • 46