Printing A SSRS Report Programmatically From WinForms ?
Hi,
I have a "simple" requirement to programmatically print a SSRS report from within a WinForms application. In this case the report is a label printed in response to the user making a specific action within the application.
I am currently using the report viewer but I dont need the report to be visible on the screen, and I dont want any user intervention to print the report, I just want to be able to print it programatically.
Despite a number of examples in the web, many appear to be some years old and refer to outdated versions of SQL. I am currently using VS2010 and SQL2008.
Can anybody give me an example ( in C# ideally ) as to how to achieve this ?
Any help / advice would be gratefully received.
With many thanks,
Nick
October 8th, 2011 5:54am
Hi Nick Taylor,
See http://printssrsreport.blogspot.com/Sergei
Free Windows Admin Tool Kit Click here and download it now
October 9th, 2011 5:42am
Hi try this
public void OpenReport()
{
int numberOfPages = 0;
FileLocation = Path.GetTempPath() + "PrintReport.pdf";
SaveReport("PDF", FileLocation);
using (StreamReader sr = new StreamReader(File.OpenRead(FileLocation)))
{
Regex regex = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
numberOfPages = matches.Count;
}
if (File.Exists(FileLocation))
File.Delete(FileLocation);
printDialogReport.PrinterSettings.FromPage = 1;
printDialogReport.PrinterSettings.ToPage = numberOfPages;
printDialogReport.PrinterSettings.PrintRange = PrintRange.AllPages;
printDialogReport.PrinterSettings.DefaultPageSettings.Landscape = PageLandscape;
if (printDialogReport.ShowDialog() != DialogResult.Cancel)
{
ReportPrintDocument PrintRep = new ReportPrintDocument(rViewer.ServerReport,
printDialogReport.PrinterSettings.FromPage,
printDialogReport.PrinterSettings.ToPage,
printDialogReport.PrinterSettings.DefaultPageSettings.Landscape);
PrintRep.Print();
}
else
{
this.Cancel = true;
}
}
public void SaveReport(string format, string FileLocation)
{
const string deviceInfo = null;
Byte[] bytPDF;
string encoding;
string mimeType;
string extension;
Warning[] warnings;
string[] streamIDs;
bytPDF = rViewer.ServerReport.Render(format, deviceInfo, out extension, out encoding, out mimeType, out streamIDs,
out warnings);
using (FileStream stream = File.OpenWrite(FileLocation))
{
stream.Write(bytPDF, 0, bytPDF.Length);
}
}
NLoly
May 17th, 2012 12:20pm