Thursday, January 8, 2009

Printing Report Viewer Local Report in FIRE FOX

Hi,
A major issue in the report viewer is that its print functionality does not work in Fire Fox. It does not display the icon of the print so we have to add our own control in the toolbar of report viewer. Previously I have told how to add control in the toolbar now this code will help you to make it active for printing.

Declare IList stream above the page load event.
private IList m_streams;

Add the below functions into your class

private Stream CreateStream(string name,string fileNameExtension, Encoding encoding,string mimeType, bool willSeek)
{
Stream stream = new FileStream(@"..\..\" + name +
"." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo =
"" +
" EMF" +
" 8.5in" +
" 11in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
"
";
Warning[] warnings;
m_streams = new List();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
// Handler for PrintPageEvents
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);
}

private void Print()
{

//const string printerName =
// "Microsoft Office Document Image Writer";


if (m_streams == null m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
//printDoc.PrinterSettings.PrinterName = printerName;
printDoc.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters[0].ToString();

if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format(
"Can't find printer \"{0}\".", printerName);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}

public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}


Assign the below function to your control which you have added in your toolbar.

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
DataSet ds = //data set containg the data tables
DataTable dt = data table containing the data

LocalReport report = new LocalReport();
report.EnableHyperlinks = true;
report.ReportPath = Server.MapPath("path of ur local report ");
report.DataSources.Add(new ReportDataSource("Datasource of the local report", Local data table declared above dt));

Export(report);
m_currentPageIndex = 0;
Print();
}

I have done the changes in the code available on the msdn help of local report printing.

Reference link: http://msdn.microsoft.com/en-us/library/ms252091.aspx

Thanks

Adding a control in Report Viewer Toolbar

You can add any asp.net control in report viewer toolbar by using the following code and can writer client side and server side events.


foreach (Control c in ReportViewer1.Controls)
{
if (c.ToString() == "Microsoft.Reporting.WebForms.ToolbarControl")
{
foreach (Control cc in c.Controls)
{
if (cc.ToString() == "Microsoft.Reporting.WebForms.ExportGroup")
{
ImageButton imgButton = new ImageButton();
imgButton.ID = "img1";
imgButton.CausesValidation = false;
imgButton.ImageUrl = "printer_icon.gif";
string targeturl = "http://localhost:1537" + Request.RawUrl.ToString();
imgButton.OnClientClick = "javascript:return OpenNewWindow('" + targeturl + "')";
//OpenNewWindow is a function written in javascript of the page that opens a new window using window.open
imgButton.Click += new ImageClickEventHandler(ImageButton1_Click);
imgButton.ToolTip = "Print";
cc.Controls.Add(imgButton);


}
}
}
}