HOW TO: Print A Report Directly To A Printer

Oct 18, 2006

The code below is a class file done in vb.net. The original idea came from reading this forum and some blogs.



To use the code below, you can create a windows application or service.

then create a class file and drop this code in it.

Remeber to reference the 2005 report execution service and also in the program settings include the path to your server.

IE: ReportExecutionService = http://localhost/ReportServer/ReportExecution2005.asmx or whatever your server URL is at.

Setup the public properties for printername (sharenames work fine), Number of copies and Report name.

That is all there is to it. This code is REALLY expandable to add more options.

Please remember to let me kow if you like this.

Imports System

Imports System.Drawing

Imports System.Drawing.Imaging

Imports System.Drawing.Printing

Imports System.IO

Imports System.Web.Services.Protocols

Imports PrintReport.ReportExecution

Imports System.Runtime.InteropServices ' For Marshal.Copy

Namespace PrintReport

Friend Class app

Private m_sPrinterName As String

Private m_sReportName As String

Private m_sNumCopies As Integer

<STAThread()> _

Public Sub Main(ByVal args As String())

Dim pe As PrintMain = New PrintMain()

pe.PrintReport(m_sPrinterName, m_sReportName, m_sNumCopies)

End Sub

Public Property pPrinterName()

Get

Return m_sPrinterName

End Get

Set(ByVal value)

m_sPrinterName = value

End Set

End Property

Public Property pReportName()

Get

Return m_sReportName

End Get

Set(ByVal value)

m_sReportName = value

End Set

End Property

Public Property pNumCopies()

Get

Return m_sNumCopies

End Get

Set(ByVal value)

m_sNumCopies = value

End Set

End Property

End Class

Friend Class PrintMain

Private rs As New ReportExecutionService()

Private m_renderedReport As Byte()()

Private m_delegate As Graphics.EnumerateMetafileProc = Nothing

Private m_currentPageStream As MemoryStream

Private m_metafile As Metafile = Nothing

Private m_numberOfPages As Integer

Private m_currentPrintingPage As Integer

Private m_lastPrintingPage As Integer

Public Sub New()

' Create proxy object and authenticate

rs.Credentials = System.Net.CredentialCache.DefaultCredentials

rs.Url = My.Settings.ReportExecutionService '"http://localhost/ReportServer/ReportExecution2005.asmx"

End Sub

Public Function RenderReport(ByVal reportPath As String) As Byte()()

' Private variables for rendering

Dim deviceInfo As String

Dim format As String = "IMAGE"

Dim firstPage As Byte() = Nothing

Dim encoding As String = ""

Dim mimeType As String = ""

Dim warnings As Warning() = Nothing

Dim reportHistoryParameters As ParameterValue() = Nothing

Dim streamIDs As String() = Nothing

Dim pages As Byte()() = Nothing

Dim historyID As String = Nothing

Dim showHideToggle As String = Nothing

Dim execInfo As New ExecutionInfo

Dim execHeader As New ExecutionHeader()

Dim SessionId As String

Dim extension As String = ""

rs.ExecutionHeaderValue = execHeader

execInfo = rs.LoadReport(reportPath, historyID)

'rs.SetExecutionParameters(parameters, "en-us")

SessionId = rs.ExecutionHeaderValue.ExecutionID

' Build device info based on the start page

deviceInfo = String.Format("<DeviceInfo><OutputFormat>{0}</OutputFormat></DeviceInfo>", "emf")

'Exectute the report and get page count.

Try

' Renders the first page of the report and returns streamIDs for

' subsequent pages

firstPage = rs.Render(format, deviceInfo, extension, encoding, mimeType, warnings, streamIDs)

' The total number of pages of the report is 1 + the streamIDs

m_numberOfPages = streamIDs.Length + 1

pages = New Byte(m_numberOfPages - 1)() {}

' The first page was already rendered

pages(0) = firstPage

Dim pageIndex As Integer = 1

Do While pageIndex < m_numberOfPages

' Build device info based on start page

deviceInfo = String.Format("<DeviceInfo><OutputFormat>{0}</OutputFormat><StartPage>{1}</StartPage></DeviceInfo>", "emf", pageIndex + 1)

pages(pageIndex) = rs.Render(format, deviceInfo, extension, encoding, mimeType, warnings, streamIDs)

pageIndex += 1

Loop

Catch ex As SoapException

'Console.WriteLine(ex.Detail.InnerXml)

Catch ex As Exception

'Console.WriteLine(ex.Message)

Finally

'Console.WriteLine("Number of pages: {0}", pages.Length)

End Try

Return pages

End Function

Public Function PrintReport(ByVal printerName As String, ByVal ReportName As String, Optional ByVal NumCopies As Integer = 0) As Boolean

Me.RenderedReport = Me.RenderReport(ReportName)

Try

' Wait for the report to completely render.

If m_numberOfPages < 1 Then

Return False

End If

Dim printerSettings As PrinterSettings = New PrinterSettings()

printerSettings.MaximumPage = m_numberOfPages

printerSettings.MinimumPage = 1

printerSettings.PrintRange = PrintRange.SomePages

printerSettings.FromPage = 1

printerSettings.ToPage = m_numberOfPages

printerSettings.Copies = NumCopies

printerSettings.PrinterName = printerName

Dim pd As PrintDocument = New PrintDocument()

m_currentPrintingPage = 1

m_lastPrintingPage = m_numberOfPages

pd.PrinterSettings = printerSettings

' Print report

'Console.WriteLine("Printing report...")

AddHandler pd.PrintPage, AddressOf pd_PrintPage

pd.Print()

Catch ex As Exception

'Console.WriteLine(ex.Message)

Finally

' Clean up goes here.

End Try

Return True

End Function

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

ev.HasMorePages = False

If m_currentPrintingPage <= m_lastPrintingPage AndAlso MoveToPage(m_currentPrintingPage) Then

' Draw the page

ReportDrawPage(ev.Graphics)

' If the next page is less than or equal to the last page,

' print another page.

If m_currentPrintingPage <= m_lastPrintingPage Then

m_currentPrintingPage += 1

ev.HasMorePages = True

End If

End If

End Sub

' Method to draw the current emf memory stream

Private Sub ReportDrawPage(ByVal g As Graphics)

If Nothing Is m_currentPageStream OrElse 0 = m_currentPageStream.Length OrElse Nothing Is m_metafile Then

Return

End If

SyncLock Me

' Set the metafile delegate.

Dim width As Integer = m_metafile.Width

Dim height As Integer = m_metafile.Height

m_delegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback)

' Draw in the rectangle

Dim destPoint As Point = New Point(0, 0)

g.EnumerateMetafile(m_metafile, destPoint, m_delegate)

' Clean up

m_delegate = Nothing

End SyncLock

End Sub

Private Function MoveToPage(ByVal page As Int32) As Boolean

' Check to make sure that the current page exists in

' the array list

If Nothing Is Me.RenderedReport(m_currentPrintingPage - 1) Then

Return False

End If

' Set current page stream equal to the rendered page

m_currentPageStream = New MemoryStream(Me.RenderedReport(m_currentPrintingPage - 1))

' Set its postion to start.

m_currentPageStream.Position = 0

' Initialize the metafile

If Not Nothing Is m_metafile Then

m_metafile.Dispose()

m_metafile = Nothing

End If

' Load the metafile image for this page

m_metafile = New Metafile(CType(m_currentPageStream, Stream))

Return True

End Function

Private Function MetafileCallback(ByVal recordType As EmfPlusRecordType, ByVal flags As Integer, ByVal dataSize As Integer, ByVal data As IntPtr, ByVal callbackData As PlayRecordCallback) As Boolean

Dim dataArray As Byte() = Nothing

' Dance around unmanaged code.

If data <> IntPtr.Zero Then

' Copy the unmanaged record to a managed byte buffer

' that can be used by PlayRecord.

dataArray = New Byte(dataSize - 1) {}

Marshal.Copy(data, dataArray, 0, dataSize)

End If

' play the record.

m_metafile.PlayRecord(recordType, flags, dataSize, dataArray)

Return True

End Function

Public Property RenderedReport() As Byte()()

Get

Return m_renderedReport

End Get

Set(ByVal value As Byte()())

m_renderedReport = value

End Set

End Property



End Class

end namespace









View 27 Replies


ADVERTISEMENT

Problem With Printing Report Directly To Printer

Aug 22, 2007

Hello,
I am trying to get a SSRS 2005 report to print from my Visual Studio 2005 C++ application without using the ReportViewer to preview it first. What I have done is created a dll that I call into when I want to access a certain report and print it. While searching around on the internet I found an MSDN article about printing a report without previewing and it had an example in C# code. So I used that as a guide for my C++ code but I am still having problems with rendering the report properly so it can be printed. When I try to render a report using the "Image" format, my streamid string is empty but the byte array that the render routine returns is not. Here is the code I am using, what could be the problem here?

Note: m_Streams is define elsewhere as
array<String^>^ m_Streams = gcnew array<String^>(10);


void Print::Export(LocalReport^ report)

{

array<Warning^>^ Warn = gcnew array<Warning^>(10);

String^ deviceinfo =

"<DeviceInfo>" +

" <OutputFormat>EMF</OutputFormat>" +

" <PageWidth>8.5in</PageWidth>" +

" <PageHeight>11in</PageHeight>" +

" <MarginTop>0.25in</MarginTop>" +

" <MarginLeft>0.25in</MarginLeft>" +

" <MarginRight>0.25in</MarginRight>" +

" <MarginBottom>0.25in</MarginBottom>" +

"</DeviceInfo>";

String^ mimeType;

String^ enc;

String^ FileExt;

array<Byte>^ bytes;

bytes = report->Render("Image",deviceinfo, mimeType, enc, FileExt, m_Streams,Warn); // m_Streams has a length of

return; // 0 after the Render

}


void Print:: PrintPage(System:: Object^ sender, System:: Drawing:: Printing:: PrintPageEventArgs^ ev)

{

Metafile^ pageImage = gcnew Metafile(m_Streams[m_CurrentPage]);

ev->Graphics->DrawImage(pageImage, ev->PageBounds);

m_CurrentPage++;

ev->HasMorePages = (m_CurrentPage < m_Streams->Length);

return;

}


void Print:rintRpt()

{

String^ printerName = "Default";

if (m_Streams->Length < 0)

return;

PrintDocument^ printDoc = gcnew PrintDocument();

if (!printDoc->PrinterSettings->IsValid) {

return;

}

printDoc->PrintPage += gcnew PrintPageEventHandler(this, &Print:: PrintPage);

printDoc->Print();

return;

}


void Print::Run()

{

LocalReport^ report = gcnew LocalReport();

DataSet^ ds = gcnew DataSet();

LoadData(ds);

report->ReportPath = "c:\bmi\bulrpt\Report1.rdlc";

ReportDataSource^ RDS = gcnew ReportDataSource();

RDS->Name = "DataSet1_Subject";

RDS->Value = ds->Tables["Subject"];

report->DataSources->Add(RDS);

Export(report);

PrintRpt();

return;

}

DataTable^ Print:: LoadData(DataSet^ ds)

{

System:: String ^ConnStr = "SELECT * FROM Subject";

SqlConnection^ conn = gcnew SqlConnection("Data Source=JOE-PC\BMIMSDESERVER; Initial Catalog=stx52013;Integrated Security = TRUE");

SqlCommand^ command = gcnew SqlCommand(ConnStr, conn);

SqlDataAdapter^ adapt = gcnew SqlDataAdapter(command);

adapt->TableMappings->Add("Table", "Subject");

conn->Open();

adapt->Fill(ds);

return ds->Tables["Subject"];

}

View 2 Replies View Related

Automatic Extract Data From Database, Then Generate A Report &&amp; Print It Directly

May 7, 2008

hi lads,


what i need to do for my project are as following:

a mobile user send data via GPRS to SQL server Database. then i need to have a method to detect while a particular table is being inserted. and then extract data from table construct a report dynamiclly. what should i do to achieve this goal? e.g. window application, store procedure or trigger ?

PS : client side is a mobile application developed using MCL. i don't in which way he will send all data to SQL server Database, so what i need to do is to monitor new data inserting.



2) how to auto generate and print report directly after record been inserted into the table ? Do i need to import report web service API ? if yes, which one? or i can use other methods e.g. predefine report control view in my window application, turn off pop-up menu while printing a report(I guess)

thanks

View 2 Replies View Related

Instructing A Printer To Print In Landscape Mode?

Feb 5, 2008

My company has a .Net app that embeds the reportwriter for displaying (and printing) reports. This seems to work fine visa vis printing reports in landscape mode, although odly if you look at the printer properties it SAYS it is priting in portrait. In any case, the problem comes when this default reportwriter isn't used but rather an rdl file is generated programmatically. No matter what width and height settings we use, the document unfailingly prints in portrait mode. I've looked to see whether there is any way to force the printer mode, but it seems there isn't, at least not through an rdl. Or what SHOULD work (setting the width larger than the heigh) does not. I'm wondering if anyone else here has run into difficulties like this if there are any solutions or places I might look for answers? Thanks!

View 1 Replies View Related

Define Printer In Report

Apr 17, 2007

I have a client who tells me in Access you could assign the printer directly to the report using PrtDevNames and PrtDevMode. Apparently it's true.



Can this be done in Reporting Services?



View 2 Replies View Related

How Do Set Printer Delivery Extension In RS2005 For Report Subscription

Mar 14, 2007

Can anyone help on this topic...

i need to set Printer Delivery Extension, and create a subscription for report which will be sent to the printer automatically

View 1 Replies View Related

Get A Report Directly From Excel

Dec 6, 2007



Hi,

Is possible to get a report directly from excel or I must develop an tools for that?

Fredo

View 4 Replies View Related

How To Display Report In Pdf Format Directly

May 5, 2008



Hi all
I am using local reports.(.rdlc files)
My requirement is when the user selects some options in the form and click on Submit buttion,He should get report in PDF format directly.Is it possible?if possible give me some example code.No need to preview in general format.
Help me.
Regards.

View 3 Replies View Related

Printing Report Directly Without Using Reportviewer

Apr 11, 2008



Hi,

My Requirement is to dirctly Print the SSRS Report without using Reportviewer.
If anybody know How to Print Report without using Reportviewer, please let me know asap.

Thanks in advance.

View 2 Replies View Related

Export In Csv Format Directly From The Link Of The Report

Apr 5, 2007

I have a requirement to export certain data directly in csv format from the link thats in the report interface.



Though i am able to open new window report from the link by passing the needed parameter is there any way that we can directly open the excel from the link in data table in report..

View 1 Replies View Related

Rendering Subscribed Report Directly Into Outlook

Feb 27, 2008



i have a report that is on a subscription to my team members that i want to be rendered in the actual email that they open up. i do not want them to have to click on the pdf or excel attachment to open it up, i just want the content of the report to be in the email itself in outlook.

is this possible in SSRS 05?

View 3 Replies View Related

URL Access Directly To Subscription Page Of A Perticular Report.

Dec 18, 2007



Hi,

I have a .net application in this application I want to call directly subscription page of a perticular Report through URL.
And i want to hide the header section too.Is it possible
If not how can i provide this feature of subscription in my application

Regards

View 1 Replies View Related

How To Use The Render Method To Save A Report Directly To Disk ?

May 2, 2007

Hi there,



Is there a way to programmatically save a RS results into Excel format using the render method ?

I had read about that capability but I can't seem to find any sample code on how to do it. Is this a parameter that you have to set in the render method ?

Any suggestion or tips are much appreciated !

Thanks !

View 5 Replies View Related

Reporting Services :: SSRS / How To Print A Report Without Bringing Up The Rendered Report

Jun 29, 2015

I'm wondering how to print a SSRS report without bringing up a report viewer. Is there way to print(PDF format) the SSRS report in the web application( .net )  directly from the "print" button without bringing up a report viewer?

View 2 Replies View Related

Generating A Pdf Report From Reporting Services Directly From Sql Server Stored Procedure

Dec 12, 2007

Hi

I was wondering if it was possible to call reporting server web service directly from my sql server stored procedure. The call that I need to make to reporting web service needs to generate the report in a PDF format.

View 1 Replies View Related

Report Designer &&> PRINT ONLY A SECTION OF THE REPORT

Oct 18, 2007

Hi,
I dont know if theres a way to print a section of some report, for example a summary report, where I have several charts and tables, and I want to print just the first 2 charts
Could this be done by mapping the document or separating it in some other way?

I tried using 'SubReports' control, but this way I need to have for example the first two charts in a different report (lets say 2charts.rdl), and then in the summary.rdl, add the control and set the link to 2charts.rdl,.... but I think this is pretty annoying considering the future modifications of the format in the 2charts.rdl...

Thanks for your help...

Regards

-Edith Colegio

View 1 Replies View Related

Print SSRS Report?

Mar 27, 2008

is there any way to schedule to print reports every morning from ssrs?

View 1 Replies View Related

Print Report From SQL 2000

Jul 20, 2005

hiCould someone please help me in following:I need to update Policy status base upon commencement date and thenprint with new Policy status.I written a stored procedure to update the policy.Now I need to work out how I can print a report. We already doing thereport manually in our VB6 program but this is automatic process. So nousers involve here. It would change the status by running the storedprocedure and then print the report.Would it be possible execute the VB6 program via stored procedure?Would it be possible SQL 2000 produce a report?We used active-x dll in our VB6 program, is it any way can call dll filevia stored procedure?thank you.*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!

View 1 Replies View Related

Print Report By Using SQL 2000

Jul 20, 2005

hiHow I could print a report on printer using SQL 2000?I am not going in detail about the report as at the moment I need toknow how produce reports on printer.Thank you*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!

View 1 Replies View Related

Add A Print Button To A Report

Feb 28, 2008



How do i add a print button to my report? Do i have to use ASP.NET?
(I dont know .NET)
Is there anyother way?

Thanks

View 7 Replies View Related

Print A Reportingservices Report From C#

Feb 18, 2007

hi all.

in my server i have a reportingservice report.

how can i print it from c# code?

View 2 Replies View Related

Print Report From C# Code

Feb 8, 2007

hi all.

how can i send a report, (stored on the reporting server) to print?

i know i can render it, but i want to print it , is it possible?

View 1 Replies View Related

Using 1 Report To Print In Both Letter And A4

Aug 7, 2007

I am looking for a way to, at execution, have 1 single report either print in "letter" or "A4" depending on a parameter I send it... anyone have an idea on where to start on this?

Thank you,

Brian

View 6 Replies View Related

Problem Print Report From Vb App

Jan 10, 2007

we have a vb app that displays reports yet when you try and print the report it either doesn't do so (although it appears to queue it) or it prints but with info missing and control info in odd spots. Anyone have any clues as to how to resolve this? My reports do me no good if the developers can't make the apps access them via the applications they are developing.

View 1 Replies View Related

Parameter In The Report Url To Print

Aug 19, 2007

Hi all!

Is there any parameter that we can pass in the report Url to directly print a report?

As we give Format=Excel for exporting the report to excel, is there any parameter to Print.

View 3 Replies View Related

Programmaticly Print A Report

Jun 14, 2007

hi all.

how can i print a report from c# code?

the report is stored on the reporting server.

View 2 Replies View Related

Can I Cause Parts Of The Report To NOT Print?

Apr 30, 2008


I want to add some information to a report that I do NOT want to be included in the printed output. Is there any way to designate a rectangle or list or table to NOT print?

Thanks.

View 1 Replies View Related

Print SERVER Report Without Preview

Feb 1, 2008

Does anyone know, or direct me to an example of, how to print a SERVER report directly to a printer from a windows application? There are plenty of examples on the internet that shows how to do this with a local report but a SERVER report seems to be a whole other animal.

Thanks,
Mark

View 4 Replies View Related

Report Layout Is Different In Print Preview

May 4, 2007



Hi there,



I'm working on an invoice report, that requires borders around the whole report. I've got header, footer and body in the report. In the body section, i've got a table which shows line items for an invoice. So the table grows as per the no. of lines in the invoice. As said before I've got to have border around the report. I tried put info in ractangles with borders for header/footer and table with borders in body section. The borders appears fine for headers and footer ractangles but as the table grows/shrinks, border from body section is not continuous to the footer section. i.e. For an invoice with 2 line items the borders for table will appear for two lines only but i want it to be scratched up to the footer area regardless of no. of line items in the invoice.



I've also tried to use borders for header, footer and body sections itself rather than using ractangles. But then when i export the report to pdf and print from pdf, it looks fine but when i tried to print from report manger its missing right hand side border for page header, footer and body. I've made sure there is enough margin around report. Report body/header/footer are 19 cm wide that's mean the report still have 2 cm of space for margins. The left/right margin are 0.5 cm.



I've been trying to do various things for last 2 weeks but not getting anywhere. Can i anyone please help into this?



Thanks,



Vivek

View 1 Replies View Related

Print Report With Full Page

Aug 24, 2007

Hi All,

I set my report page width as 13 inch(page size=13in, 11in). Report is working correctly, It is having all the columns when I am exporting the report to PDF. But some columns are missing when I am directly printing the report. Can you please let me know how to print report with all the columns.


Thanks

View 8 Replies View Related

Print From Report Manager In Landscape

Sep 23, 2005

I've thrown together a homegrown Reporting Services viewer that is basically a wrapper for a browser control. It will reside in our app alongside the Crystal report viewer until we have phased out Crystal.

View 3 Replies View Related

Print Out Report By A4 Size Paper

Jul 31, 2007

Hi,

I have create a report with A3 size as there are too many fields., but i
wanna to print out on A4 paper properly. is that possible to do?


Cheers

Nick

View 3 Replies View Related

Report Viewer Print Functionality Not Working

May 11, 2008

Hi

I have designed a webpage which has a tabstrip and each tabstrip is linked to a reportviewer.
When I click on the very first tab , the print button in reportviewer toolbar is working just fine!!.
But when I select another tab and click on Print button.,it gives a javascript error stating "Permission Denied",
and the print button stops responding after that.
Any clue!!,or this is a bug in report viewer print button.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved