Print directly from SSRS2008

T

Tim Kelley

I am using the following code to print a report directly from Reporting
Services 2008. When the report prints though, the font size is very large
the the report does not fit on the page. The code works fine for Reporting
Services 2005. Any ideas on what I am doing wrong?

private void button1_Click(object sender, EventArgs e)
{
ParameterValue[] parms = new ParameterValue[2];

parms[0] = new ParameterValue();
parms[0].Name = "p1";
parms[0].Value = "00-109-18751";
parms[1] = new ParameterValue();
parms[1].Name = "p2";
parms[1].Value = "9/20/2009";


PrintReport("/MyReport", parms, false);

}
private byte[][] m_renderedReport;
private Graphics.EnumerateMetafileProc m_delegate = null;
private MemoryStream m_currentPageStream;
private Metafile m_metafile = null;
int m_numberOfPages;
private int m_currentPrintingPage;
private int m_lastPrintingPage;
private string _printer = "";
PrintChecks10.WebReference.ReportExecutionService rs;


public ReportPrinter()
{
PrintDialog dlgPrint = new PrintDialog();
dlgPrint.PrinterSettings = new PrinterSettings();
if (gPrinterToUse == "" || gPrinterToUse == null)
{
if (dlgPrint.ShowDialog() == DialogResult.OK)
{
_printer = dlgPrint.PrinterSettings.PrinterName;
gPrinterToUse = _printer;
}
else
{
return;
}
}
else
{
_printer = gPrinterToUse;
}
private byte[][] RenderReport(string reportPath, ParameterValue[]
reportParameters)
{
// Private variables for rendering
string deviceInfo = null;
string format = "IMAGE";
Byte[] firstPage = null;
string encoding;
string mimeType;
Warning[] warnings = null;
ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
Byte[][] pages = null;
string historyID = null;
string extension;

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials =
System.Net.CredentialCache.DefaultCredentials;
rs.Url =
"http://mysqlserver/reportserver/ReportExecution2005.asmx";

// Build device info based on the start page
deviceInfo =
String.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.3in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>",
"emf");
ExecutionInfo execInfo = new ExecutionInfo();

ExecutionHeader execHeader = new ExecutionHeader();
rs.ExecutionHeaderValue = execHeader;

execInfo = rs.LoadReport(reportPath, historyID);

rs.SetExecutionParameters(reportParameters, "en-us");

//Execute the report and get page count.
try
{
// Renders the first page of the report and returns
streamIDs for
// subsequent pages
firstPage = rs.Render(format,
deviceInfo,
out extension,
out encoding,
out mimeType,
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++)
{
Application.DoEvents();
// 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(format,
deviceInfo,
out extension,
out encoding,
out mimeType,
out warnings,
out streamIDs);
}
}
catch (SoapException ex)
{
MessageBox.Show(ex.Detail.InnerXml);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.InnerException +
Environment.NewLine + ex.Message +
Environment.NewLine + "Number of pages: " +
pages.Length.ToString() +
Environment.NewLine + "Report Path: " + reportPath,
"Error Printing");
}
finally
{
//Console.WriteLine("Number of pages: {0}",
pages.Length);
}
return pages;
}

internal bool PrintReport(string report, ParameterValue[]
reportParameters, bool landscape)
{
this.RenderedReport = this.RenderReport(report,
reportParameters);
try
{
// Wait for the report to completely render.
if (m_numberOfPages < 1)
return false;
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.MaximumPage = m_numberOfPages;
printerSettings.MinimumPage = 1;
printerSettings.PrintRange = PrintRange.SomePages;
printerSettings.FromPage = 1;
printerSettings.ToPage = m_numberOfPages;
printerSettings.PrinterName = _printer;

PrintDocument pd = new PrintDocument();
m_currentPrintingPage = 1;
m_lastPrintingPage = m_numberOfPages;
pd.PrinterSettings = printerSettings;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0,
0);
pd.DefaultPageSettings.Landscape = landscape;

// Print report
//Console.WriteLine("Printing report...");
pd.PrintPage += new
PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Clean up goes here.
}
return true;
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = false;
if (m_currentPrintingPage <= m_lastPrintingPage &&
MoveToPage(m_currentPrintingPage))
{
// Draw the page
ReportDrawPage(ev.Graphics);
// If the next page is less than or equal to the last
page,
// print another page.
if (++m_currentPrintingPage <= m_lastPrintingPage)
ev.HasMorePages = 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);

Point[] destPoint = new Point[3];
Point point1 = new Point(0, 0);
Point point2 = new Point(width, 0);
Point point3 = new Point(0, height);

destPoint[0] = point1;
destPoint[1] = point2;
destPoint[2] = point3;


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;
}
internal byte[][] RenderedReport
{
get
{
return m_renderedReport;
}
set
{
m_renderedReport = value;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top