次の方法で共有


Controlling Page Size in a Reporting Services Report

The default page size in the US is Letter, 8.5in x 11in.  In other parts of the world A4 (8.3in x 11.7in) is the standard.  Reports in Reporting Services are hard coded as part of the design process.  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.  The Device Info Settings for a PDF document can be found here.

If we are using the Report Viewer control to display the reports we will need to disable the ability to print a report and potentially disable exporting the report, depending on the use cases we want to support.  To still provide this capability to our users we can expose the print and export properties using a link button or other postback capable control.  We can also expose the available print and export formats using a drop down or other mechanism.  Now when the user chooses to print or export the report we can override the process by passing the device info settings for the page height and width when the report is rendered.  In the example below we are exporting to PDF and passing the page height and width to match an A4 paper size as the targeted device (line 66).

    1: private void RenderReportToClient()
    2: {
    3:     //set credentials
    4:     RSExecuteProxy.ReportExecutionService rs = new RSExecuteProxy.ReportExecutionService();
    5:     rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
    6:  
    7:     RSProxy.ReportingService2005 rsInfo = new RSProxy.ReportingService2005();
    8:     rsInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;
    9:  
   10:     // init render args
   11:     byte[] result = null;
   12:     string reportPath = rptViewer.ServerReport.ReportPath;
   13:     string format = "PDF";
   14:     string historyId = null;
   15:  
   16:     string encoding;
   17:     string mimeType;
   18:     string extension;
   19:     RSExecuteProxy.Warning[] warnings = null;
   20:     string[] streamIDs = null;
   21:  
   22:     //init exec info
   23:     RSExecuteProxy.ExecutionInfo execInfo = new RSExecuteProxy.ExecutionInfo();
   24:     RSExecuteProxy.ExecutionHeader execHeader = new RSExecuteProxy.ExecutionHeader();
   25:  
   26:     rs.ExecutionHeaderValue = execHeader;
   27:  
   28:     //get report
   29:     execInfo = rs.LoadReport(reportPath, historyId);
   30:  
   31:     String SessionId = rs.ExecutionHeaderValue.ExecutionID;
   32:  
   33:     //get parameter info
   34:     ReportParameterInfoCollection parameters = rptViewer.ServerReport.GetParameters();
   35:  
   36:     //figure out how many parameters we will have 
   37:     //those with multi-value will need there own ParameterValue in the array
   38:     int paramCount = 0;
   39:  
   40:     foreach (ReportParameterInfo pramInfo in parameters)
   41:     {
   42:         paramCount += pramInfo.Values.Count;
   43:     }
   44:  
   45:  
   46:     RSExecuteProxy.ParameterValue[] prams = new SSRSWeb.RSExecuteProxy.ParameterValue[paramCount];
   47:  
   48:     int currentPramPosition = 0;
   49:  
   50:     //set pram values
   51:     foreach (ReportParameterInfo pramInfo in parameters)
   52:     {
   53:         foreach (string pramValue in pramInfo.Values)
   54:         {
   55:             prams[currentPramPosition] = new SSRSWeb.RSExecuteProxy.ParameterValue();
   56:             prams[currentPramPosition].Label = pramInfo.Name;
   57:             prams[currentPramPosition].Name = pramInfo.Name;
   58:             prams[currentPramPosition].Value = pramValue;
   59:             currentPramPosition++;
   60:         }
   61:     }
   62:  
   63:     rs.SetExecutionParameters(prams, "en-US");
   64:  
   65:     //build the device settings  (A4 8.3 × 11.7)
   66:     string deviceInfo = string.Format("<DeviceInfo><PageHeight>{0}</PageHeight><PageWidth>{1}</PageWidth></DeviceInfo>", "11.7in", "8.3in");
   67:  
   68:     //get report bytes
   69:     result = rs.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
   70:  
   71:     Response.ClearContent();
   72:     Response.AppendHeader("Content-Disposition", "inline;filename=report.pdf");
   73:     Response.AppendHeader("content-length", result.Length.ToString());
   74:     Response.ContentType = "application/pdf";
   75:     Response.BinaryWrite(result);
   76:     Response.Flush();
   77:     Response.Close();
   78: }

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.

Comments

  • Anonymous
    November 12, 2008
    PingBack from http://www.tmao.info/controlling-page-size-in-a-reporting-services-report/
  • Anonymous
    October 19, 2011
    this blog is really good! however, I have additional query regarding the page size. Currently, we are supporting for multicultural settings, how to set the page size in SSRS to supprot multicultural settings? I believe, page size is based on the client machine's regional settings, and if this size is not properly set, white spaces/pages would result when we export to pdf. We are using SSRS 2005. Any help would be greatly appreciated. Thank you.
  • Anonymous
    October 20, 2011
    Page size is done at design time and is not an expression based property.  In your situation you have to create the report for the lowest common denominator.  In this case, 8.5 x 11, and then at render time pass the proper device information settings to the report.Another option is RDCE, although it isn't avail. in SSRS 2005.  This let's you modify the report definition at run time.  You could use this technique to change the report height / width when the report is requested.  the design time would still have to be done to lowest common height and width.msdn.microsoft.com/.../cc281022.aspx