Solved

How can I print my rendered RS2008 report from c#?

Posted on 2009-07-04
4
606 Views
Last Modified: 2013-11-25
I have a class that renders my report and saves it to a PDF file. As well as the save to pdf I also want to send the report to a printer. (Ideally default printer).

I've attached the code below, does anyone know how to modify this to add the print feature?

I'm using VS2008, .net 3.5 and RS2008.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using BEC.Logger;
using BECScheduler.RS2005;
using BECScheduler.rsExecService;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
 
namespace ReportServerProject
{    
    public class MyReportRenderer    
    {
        
       
        private BECScheduler.RS2005.ReportingService2005 rs;        
        private BECScheduler.rsExecService.ReportExecutionService rsExec;        
        public string renderTest(string rptname,string sDate,string eDate,string printerName)       
        {            
            rs = new ReportingService2005();            
            rsExec = new ReportExecutionService();
            rs.Credentials = new System.Net.NetworkCredential("user","pass","domain");
            rsExec.Credentials = new System.Net.NetworkCredential("user","pass","domain");
            rs.Url = "http://server/ReportServer_mssql2008/ReportService2005.asmx";
            rsExec.Url = "http://server/ReportServer_mssql2008/ReportExecution2005.asmx";
            string historyID = null;            
            string deviceInfo = null;
            string format = "CSV";
            Byte[] results;
            string encoding = String.Empty;
            string mimeType = String.Empty;
            string extension = String.Empty;
            BECScheduler.rsExecService.Warning[] warnings = null;
            string[] streamIDs = null;
            // Default Path;
            string fileName = @"c:\samplereport.csv";
            string _reportName = rptname;//@"/Trend Log Reporting/TrendReport/";
            string _historyID = null; 
            bool _forRendering = false;
            BECScheduler.RS2005.ParameterValue[] _values = null;
            BECScheduler.RS2005.DataSourceCredentials[] _credentials = null;
            BECScheduler.RS2005.ReportParameter[] _parameters = null; 
            try            
            {                
                _parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials);
                BECScheduler.rsExecService.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
                BECScheduler.rsExecService.ParameterValue[] parameters = new BECScheduler.rsExecService.ParameterValue[2];
                if (_parameters.Length > 0)
                {
                  parameters[0] = new BECScheduler.rsExecService.ParameterValue();
                  parameters[0].Label = "";
                  parameters[0].Name  = "SDate"; 
                  parameters[0].Value = sDate;
                  parameters[1] = new BECScheduler.rsExecService.ParameterValue();
                  parameters[1].Label = "";
                  parameters[1].Name = "EDate";
                  parameters[1].Value = eDate; 
                }
                rsExec.SetExecutionParameters(parameters, "en-GB");
                results=  rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
 
              
                using (FileStream stream = File.OpenWrite(fileName))
                    
                {                    
                    stream.Write(results, 0, results.Length);
                    
                    
                }           
            }            
            catch (Exception ex)            
            {
                new BEC.Logger.Logger().ErrorLog(ex.Message);
                throw ex;
                           }
            return (fileName);
  
            
            
        }
    }
    }

Open in new window

0
Comment
Question by:wint100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:rob_farley
ID: 24781930
This blog entry should help.

http://geekswithblogs.net/bleepzter/archive/2008/10/09/printing-a-pdf-file-to-a-network-printer-with-c.aspx

It's not something I've actually had to do before, but it should be easy enough.

Rob
0
 
LVL 1

Author Comment

by:wint100
ID: 24787489
So that article uses the below code.

I'm new to development. How would I replace the tempDir with my directory and set it to print a specific file from a location (e.g. c:\reports)

Also, how do I call the public void into action?
using System;                                                               
using System.Collections.Generic;                                           
using System.ComponentModel;                                                
using System.Data; System.Drawing;                                          
using System.IO;                                                            
using System.Text; System.Windows.Forms;                                    
using System.Management;                                                    
using System.Management.Instrumentation;                                    
                                                                            
/* the stuff in between is not relevant that's why its not included  */     
                                                                            
private void print(ref DirectoryInfo tempDir)                               
{                                                                           
    try                                                                     
    {                                                                       
        foreach(FileInfo file in tempDir.GetFiles("*.pdf"))                 
        {                                                                   
            file.CopyTo("\\\\XYZ\\Phaser77\\" + file.Name);                 
        }                                                                   
    }                                                                       
    catch(Exception ee){ }                                                  
}     

Open in new window

0
 
LVL 1

Accepted Solution

by:
wint100 earned 0 total points
ID: 24833920
I ended up using a seperate class to render the report again in the IMAGE format, then sent this to the printer.
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24834141
Ah, ok. If that works, then great!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question