Problem With Direct Printing from Microsoft Report Viewer.
Hi All,
I am trying to print a Report viewer content (Report .rdlc file) in C#.
The Algo is given below:-
1. After generation of Report, I am making the Report content as an Image extention is .emf.
2. Saveing the report into D://Report/Voucher.emf
3. Calling Print Function.
When I an running this algo from Code then it is working properly. Saveing the Image in proper path and Printing properly.
But when I am makeing the WebSetup and Insatalling this webApplicatiion and running from IIS 6.0 then it is Generating the Image in .emf format but not printing at all.
Can any one help me in this situation.
If you know please help me.
*********************Please See the Code Below**************************
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
LocalReport lr = ReportViewer1.LocalReport;
Export(lr);
m_currentPageIndex = 0;
Print();
Dispose();
string Path = Session["Path"].ToString();
}
#region Deviceinfo
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>emf</OutputFormat>" +
" <PageWidth>8.3in</PageWidth>" +
" <PageHeight>5.0in</PageHeight>" +
" <MarginTop>0.2in</MarginTop>" +
" <MarginLeft>0.2in</MarginLeft>" +
" <MarginRight>0.0in</MarginRight>" +
" <MarginBottom>0.0in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
#endregion
#region PrintPageEvents
// 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);
}
#endregion
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream("D:\\Report\\" + name + "." + fileNameExtension, FileMode.Create);
string path = "D:\\Report\\" + name + "." + emf";
Session["Path"] = "http://192.168.0.3/"+path; // This path will use to delete the image file after printing.
m_streams.Add(stream);
return stream;
}
#region Print function
private void Print()
{
System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings();
//string printerName=
string printerName = oPS.PrinterName;
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
System.Drawing.Printing.PrinterSettings pseting = new PrinterSettings();
printDoc.PrinterSettings.PrinterName = printerName;
PrintingPermission perm = new PrintingPermission(System.Security.Permissions.Per missionState.Unrestricted);
perm.Level = PrintingPermissionLevel.AllPrinting;
if (printerName != "")
{
printDoc.PrinterSettings.PrinterName = String.Format("{0}", printerName);
}
if (printDoc.PrinterSettings.IsValid != false)
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
}
#endregion
#region Dispose the Streem
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
#endregion
#region Delete Image File
public static void Delete(string path)
{
File.Delete(path);
//Console.WriteLine("{0} was successfully deleted.", path);
}
#endregion
With Regards
Somesh Chatterjee
Last edited by someshchat; September 13th, 2011 at 02:49 AM..
|