Visual Studio Development Bookmark and Share   
 index > Visual Studio Report Controls > Printing Report without Viewer
 

Printing Report without Viewer

I recently completed the walkthrough Printing a local Report without the Viewer in a VS 2008 aspx project.  All went well until I published the website to a IIS Windows 2003 Server.   Once I found how to grant the ASP.NET service rights to create the .emf on the server I hit a roadblock. 

                                           

The first time the print request is made the .emf files are created on the server but they never print.  No exceptions or errors occur.  However, on the second attempt I get the following exception

 

The process cannot access the file 'c:\temp\rpTicketInYard_1.emf' because it is being used by another process.

 

The .emf files cannot be deleted from windows explorer logged in as admin.  I have to restart the server before I can delete the files.


Here is the calling code

 Public Sub PrintReport()

        Dim pd As New PrintIt

        Dim report As LocalReport = New LocalReport()

        report.ReportPath = "Tickets2.rdlc"

        report.EnableExternalImages = True



        Dim _t As New TpHarvestTicketV

        Dim _tl As List(Of TpHarvestTicketV) = _t.GetTicketList("WHERE harvest_ticket_number = 50810", "")

        If _tl Is Nothing Then

            MsgBox("fart")

            Return

        End If



        report.DataSources.Clear()

        Dim repsource As New ReportDataSource

        repsource.Name = "TpHarvestTicketV"

        repsource.Value = _tl



        report.DataSources.Add(repsource)

        report.Refresh()





        pd.Export(report)

        pd.m_currentPageIndex = 0



        Const printerName As String = "Star TSP700II (TSP743II)"

        If pd.m_streams Is Nothing Or pd.m_streams.Count = 0 Then

            Return

        End If



        Dim printDoc As New PrintDocument()

        If Not printDoc.PrinterSettings.IsValid Then

            Dim msg As String = String.Format("Can't find printer ""{0}"".", printerName)

            Console.WriteLine(msg)

            Return

        End If



        AddHandler printDoc.PrintPage, AddressOf pd.PrintPage

        printDoc.Print()

        printDoc.Dispose()

    End Sub



<br />

and the class

Imports System.IO

Imports System.Data

Imports System.Text

Imports System.Drawing.Imaging

Imports System.Drawing.Printing

Imports System.Collections.Generic

Imports Microsoft.Reporting.WebForms



Public Class PrintIt

    Implements IDisposable



    Public m_currentPageIndex As Integer

    Public m_streams As IList(Of Stream)



    Private Function CreateStream(ByVal name As String, _

       ByVal fileNameExtension As String, _

       ByVal encoding As Encoding, ByVal mimeType As String, _

       ByVal willSeek As Boolean) As Stream

        Dim stream As Stream = _

            New FileStream("c:\temp\" & name + "." + fileNameExtension, FileMode.Create)

        m_streams.Add(stream)

        Return stream

    End Function



    Public Sub Export(ByVal report As LocalReport)

        Dim deviceInfo As String = _

          "<DeviceInfo>" + _

          "  <OutputFormat>EMF</OutputFormat>" + _

          "  <PageWidth>8.5</PageWidth>" + _

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

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

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

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

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

          "</DeviceInfo>"

        Dim warnings() As Warning = Nothing

        m_streams = New List(Of Stream)()

        report.Render("Image", deviceInfo, AddressOf CreateStream, _

           warnings)



        Dim stream As Stream

        For Each stream In m_streams

            stream.Position = 0

        Next

    End Sub



    Public Sub PrintPage(ByVal sender As Object, _

    ByVal ev As PrintPageEventArgs)

        Dim pageImage As New Metafile(m_streams(m_currentPageIndex))

        ev.Graphics.DrawImage(pageImage, ev.PageBounds)



        m_currentPageIndex += 1

        ev.HasMorePages = (m_currentPageIndex < m_streams.Count)

    End Sub



    Public Overloads Sub Dispose() Implements IDisposable.Dispose

        If Not (m_streams Is Nothing) Then

            Dim stream As Stream

            For Each stream In m_streams

                stream.Close()

            Next

            m_streams = Nothing

        End If

    End Sub



 



End Class

  • Edited byklrklr Friday, October 16, 2009 4:28 PM
  •  
klrklr  Friday, October 16, 2009 3:03 AM
Found the Solution to stream problem.  I changed the FileStream to a MemoryStream.
  • Marked As Answer byklrklr Friday, October 16, 2009 4:28 PM
  •  
klrklr  Friday, October 16, 2009 4:26 PM
Found the Solution to stream problem.  I changed the FileStream to a MemoryStream.
  • Marked As Answer byklrklr Friday, October 16, 2009 4:28 PM
  •  
klrklr  Friday, October 16, 2009 4:26 PM

You can use google to search for other answers

Custom Search

More Threads

• Setting report parameters question
• What version of SQL RS does VS2008 target?
• Web Reference in .NET 2.0 APPLICATION
• SubReport Processing Event only firing for some of my reports
• Delay in the appearance of tooltip
• Error: Internet Explorer cannot download ...ed.ReportViewerWebControl.axd from site
• Background picture problems
• [local mode] master-details with on page per detail line
• Printing a standard invoice
• dataset and i want to print out all the records with in it