blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Steve Wright's Blog

Report Viewer Customizing Printing for Local Report

We had a requirement to allow a front page, which was another local report, to be printed before the report within the report viewer was printed.  As both reports were local reports it was easy to create a print button which allowed us to meet the requirements, here is the code that we used:

if (this.printFrontingSheetCheckbox.Checked)
{
 LocalReport frontsheet = front.reportViewer1.LocalReport;
 Export(frontsheet, 8.27f, 11.69f, 0f, 0f, 0f, 0f);
 m_currentPageIndex = 0;
 Print(false);
 
 foreach (Stream s in m_streams)
 {
  s.Close();
 }
 front = null;
}

LocalReport report = reportViewer.LocalReport;
Export(report, 11.69f, 8.27f, 0f, 0f, 0f, 0f);
m_currentPageIndex = 0;

Print(true); foreach (Stream s in m_streams)
{
 s.Close();
}

private void Print(bool landscape)
{
 if (m_streams == null || m_streams.Count == 0)
  return;
 PrintDocument printDoc = new PrintDocument();
 PrinterSettings ps = new PrinterSettings();
 ps.PrinterName = printDialog1.PrinterSettings.PrinterName;
 ps.DefaultPageSettings.Landscape = landscape;
    ps.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = landscape;
 printDoc.PrinterSettings = ps;

 if (!printDoc.PrinterSettings.IsValid)
 {
  string msg = String.Format("Can't find printer \"{0}\".", printDialog1.PrinterSettings.PrinterName);
  MessageBox.Show(msg, "Print Error");
  return;
 }
 printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
 printDoc.Print();
} /// <summary>
/// Export the given report as an EMF (Enhanced Metafile) file.
/// </summary>
private void Export(LocalReport report, float PageWidth, float PageHeight, float MarginTop, float MarginLeft, float MarginRight, float MarginBottom)
{
 StringBuilder deviceInfosb = new StringBuilder();
 deviceInfosb.Append("<DeviceInfo>");
 deviceInfosb.Append("<OutputFormat>EMF</OutputFormat>");
 deviceInfosb.Append(string.Format("<PageWidth>{0}in</PageWidth>", PageWidth));
 deviceInfosb.Append(string.Format("<PageHeight>{0}in</PageHeight>", PageHeight));
 deviceInfosb.Append(string.Format("<MarginTop>{0}in</MarginTop>", MarginTop));
 deviceInfosb.Append(string.Format("<MarginLeft>{0}in</MarginLeft>", MarginLeft));
 deviceInfosb.Append(string.Format("<MarginRight>{0}in</MarginRight>", MarginRight));
 deviceInfosb.Append(string.Format("<MarginBottom>{0}in</MarginBottom>", MarginBottom));
 deviceInfosb.Append(string.Format("</DeviceInfo>"));
 string deviceInfo = deviceInfosb.ToString();
 Microsoft.Reporting.WinForms.Warning[] warnings;
 m_streams = new List<Stream>();
 report.Render("Image", deviceInfo, CreateStream, out warnings);
 foreach (Stream stream in m_streams)
 {
  stream.Position = 0;
 }
}

/// <summary>
/// Handler for PrintPageEvents
/// </summary>
private void PrintPage(object sender, PrintPageEventArgs ev)
{
 Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
 ev.Graphics.DrawImage(pageImage, ev.PageBounds);
 m_currentPageIndex++;
 ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}

Then a new requirement came that we needed to get the report viewer print button to be the same as our print button.  At first I didn’t think it was possible because the button was within the report viewer control, but after looking through all the properties, methods and events we found the print event, which according to the object help, fires after the user prints a report.  This would help because at least we would be able to detect a print event and then print the front page at the end of printing the report.  But a collogue played around with the code and found that the event gets fired first before the printing happens and setting the cancel variable to true stops the report viewer doing the printing then by calling our printing code this allowed us to meet all the requirements.

Published 11 October 2006 19:21 by steve.wright

Comments

 

rai said:

i want to use this in my project. can i see the souce code pliz..

August 15, 2007 23:53
 

rai said:

i want to use this in my project. can look at the actual code pliz.

with control.....

August 15, 2007 23:54
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems