As many of you might have figured out by now Reporting Services 2005 doesn’t support rotating objects or multiple orientation (as in per sub report). Depending on your situation there might still be an answer. I was in a situation recently where I need certain pages of a report in landscape and some in portrait. I was told that this is not possible out of the box in Reporting Services 2005 but have since found a workaround that might work for you. Microsoft has done a great job with making SSRS an open architecture and the web services are a key part of this approach. Behind the scenes of your Report Viewer control (in “remote” mode) it is actually making a request to the Render command of the SSRS Web Service. So to pull of the rotation of a report I created a new web page with a web reference to the ReportService. I then request the report in IMAGE format and store the byte[]. I create a new Bitmap object and stream in the byte[] so that I have full control over my newly created report Image in GDI.NET. Bitmap supports a RotateFlip method that has just what we need and then I save the Bitmap to the Response.OutputStream object. You are probably familiar with the concept of pointing an Image to an ASPX page that dynamically generates the bytes of the Image and this solution is no different. Once you have an ASPX that can generate a rotated Image of the report then you treat it just like any other Image in the Report or on a WebForm.
This solution was adapted from Bryan Kelly’s post on Programmatically Printing RS 2000 Reports found HERE
The source code for the page is below:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices; // For Marshal.Copy
using System.IO;
using System.Web.Services.Protocols;
public partial class ASR_ReportAsImage : System.Web.UI.Page
{
ReportService.ReportingService rs;
private byte[][] m_renderedReport;
private System.Drawing.Graphics.EnumerateMetafileProc m_delegate = null;
private System.IO.MemoryStream m_currentPageStream;
private System.Drawing.Imaging.Metafile m_metafile = null;
int m_numberOfPages;
protected void Page_Load(object sender, EventArgs e)
{
rs = new ReportService.ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
PrintReportAsImage();
}
public byte[][] RenderReport(string reportPath)
{
// Private variables for rendering
string deviceInfo = null;
string format = “IMAGE”;
Byte[] firstPage = null;
string encoding;
string mimeType;
ReportService.Warning[] warnings = null;
ReportService.ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
Byte[][] pages = null;
// Build device info based on the start page
deviceInfo =
String.Format(@”<DeviceInfo><OutputFormat>{0}</OutputFormat></DeviceInfo>”, “emf”);
//Exectute the report and get page count.
// Renders the first page of the report and returns streamIDs for
// subsequent pages
firstPage = rs.Render(
reportPath,
format,
null,
deviceInfo,
null,
null,
null,
out encoding,
out mimeType,
out reportHistoryParameters,
out warnings,
out streamIDs);
// The total number of pages of the report is 1 + the streamIDs
m_numberOfPages = streamIDs.Length + 1;
pages = new Byte[m_numberOfPages][];
// The first page was already rendered
pages[0] = firstPage;
for (int pageIndex = 1; pageIndex < m_numberOfPages; pageIndex++)
{
// Build device info based on start page
deviceInfo =
String.Format(@”<DeviceInfo><OutputFormat>{0}</OutputFormat><StartPage>{1}</StartPage></DeviceInfo>”,
“emf”, pageIndex + 1);
pages[pageIndex] = rs.Render(
reportPath,
format,
null,
deviceInfo,
null,
null,
null,
out encoding,
out mimeType,
out reportHistoryParameters,
out warnings,
out streamIDs);
}
return pages;
}
public bool PrintReportAsImage()
{
this.RenderedReport = this.RenderReport(“/ASR Prototype/ASR_TransactionOverview”);
// Wait for the report to completely render.
if (m_numberOfPages < 1)
return false;
for (int i = 0; i < m_renderedReport.Length; i++)
{
//write all of the pages to stream….
System.IO.MemoryStream memstream = new System.IO.MemoryStream(m_renderedReport[i], false);
System.Drawing.Bitmap oBitmap = new System.Drawing.Bitmap(memstream, true);
//now rotate the bitmap 90 degrees
oBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
//write it to the output stream
Response.ContentType = “image/jpeg”;
oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
return true;
}
// Method to draw the current emf memory stream
private void ReportDrawPage(Graphics g)
{
if (null == m_currentPageStream || 0 == m_currentPageStream.Length || null == m_metafile)
return;
lock (this)
{
// Set the metafile delegate.
int width = m_metafile.Width;
int height = m_metafile.Height;
m_delegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
// Draw in the rectangle
Point destPoint = new Point(0, 0);
g.EnumerateMetafile(m_metafile, destPoint, m_delegate);
// Clean up
m_delegate = null;
}
}
private bool MoveToPage(Int32 page)
{
// Check to make sure that the current page exists in
// the array list
if (null == this.RenderedReport[m_currentPrintingPage - 1])
return false;
// Set current page stream equal to the rendered page
m_currentPageStream = new MemoryStream(this.RenderedReport[m_currentPrintingPage - 1]);
// Set its postion to start.
m_currentPageStream.Position = 0;
// Initialize the metafile
if (null != m_metafile)
{
m_metafile.Dispose();
m_metafile = null;
}
// Load the metafile image for this page
m_metafile = new Metafile((Stream)m_currentPageStream);
return true;
}
private bool MetafileCallback(
EmfPlusRecordType recordType,
int flags,
int dataSize,
IntPtr data,
PlayRecordCallback callbackData)
{
byte[] dataArray = null;
// Dance around unmanaged code.
if (data != IntPtr.Zero)
{
// Copy the unmanaged record to a managed byte buffer
// that can be used by PlayRecord.
dataArray = new byte[dataSize];
Marshal.Copy(data, dataArray, 0, dataSize);
}
// play the record.
m_metafile.PlayRecord(recordType, flags, dataSize, dataArray);
return true;
}
public byte[][] RenderedReport
{
get
{
return m_renderedReport;
}
set
{
m_renderedReport = value;
}
}
}