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
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 =
"
"
"
"
"
"
"
"
"
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
I read your blog and this is very useful. I am impressed by this. It helped me a lot. I will surely visit your blog regularly.
ReplyDeleteThanks