none
imprimir 2 veces un documento RRS feed

  • Pregunta

  • Nececito impimir una hoja 2 veces, porque el documento a imprimir es un contrato el cual el cliente se le entrega una hoja del contrato y la otra se la queda la empresa.

    Lo hago con reporting. Gracia

    miércoles, 17 de noviembre de 2010 16:36

Respuestas

  • Hola Jonathan, sigue un ejemplo para la impresión, sin utilizar el Microsoft Reports:

    Revisa la parte del cls.Print(rpt)

    Puedes utilizar:

    For I = 1 to 2 (o el numero que quieras de copias)
    cls.Print(rpt)
    Next

    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports Microsoft.Reporting.WinForms

    Public Class ReportUtils
        Implements IDisposable

        Private currentPageIndex As Integer
        Private
    tmpFileName As String = String.Empty
        Private streamList As List(Of Stream)

        '''<summary>
      
    ''' Adiciona a Stream à lista de Streams
        '''
    </summary>
      
    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

            tmpFileName = My.Computer.FileSystem.GetTempFileName()

            Dim s As New FileStream(tmpFileName, FileMode.Create)
            streamList.Add(s)
            Return s

        End Function

      
    '''<summary>
      
    ''' Exporta o ficheiro para uma lista de Streams
        '''
    </summary>
      
    Private Sub ExportToStream(ByVal report As LocalReport)
            Dim deviceInfo As New StringBuilder
            With deviceInfo
                .Append("<DeviceInfo>")
                .Append(" <OutputFormat>EMF</OutputFormat>")
                .Append(" <PageWidth>8.5in</PageWidth>")
                .Append(" <PageHeight>11in</PageHeight>")
                .Append(" <MarginTop>0.25in</MarginTop>")
                .Append(" <MarginLeft>0.25in</MarginLeft>")
                .Append(" <MarginRight>0.25in</MarginRight>")
                .Append(" <MarginBottom>0.25in</MarginBottom>")
                .Append("</DeviceInfo>")
            End With

            Dim
    warnings() As Warning = Nothing
          
    report.Render("Image", deviceInfo.ToString, _
                            AddressOf CreateStream, warnings)

            For Each s As Stream In streamList
                s.Position = 0
            Next
          
    deviceInfo = Nothing

        End Sub

      
    '''<summary>
      
    ''' Quando o PrintDocument está a imprimir, desenha
        ''' a página correspondente, da lista de Streams
        '''
    </summary>
      
    Private Sub PrintPage(ByVal sender As Object, _
                              ByVal ev As PrintPageEventArgs)

            Using pageImage As New Metafile(streamList(currentPageIndex))
                currentPageIndex += 1

                ev.Graphics.DrawImage(pageImage, ev.PageBounds)
                ev.HasMorePages = (currentPageIndex < streamList.Count)
            End Using
        End Sub

      
    '''<summary>
      
    ''' Imprime um relatório sem visualização
        '''
    </summary>
      
    '''<param name="report">Relatório a imprimir</param>
      
    Public Sub Print(ByVal report As LocalReport)

            streamList = New List(Of Stream)

            ' Exporta o ficheiro para uma lista de Streams
          
    Call ExportToStream(report)

            If streamList IsNot Nothing AndAlso streamList.Count > 0 Then

              
    ' Inicia o processo de impressão
              
    Using printDoc As New PrintDocument()

                    If Not printDoc.PrinterSettings.IsValid Then
                        Dim
    msg As String = "Impressora não disponível ou não válida!"
                      
    Throw New ArgumentException(msg)
                    End If

                    AddHandler
    printDoc.PrintPage, AddressOf PrintPage
                    printDoc.Print()
                End Using

            End If

        End Sub

        Public Overloads Sub
    Dispose() Implements IDisposable.Dispose

            Try

              
    ' Fecha as streams e apaga a lista
              
    If streamList IsNot Nothing Then
                    For Each
    s As Stream In streamList
                        s.Close()
                    Next
                  
    streamList.Clear()
                    streamList = Nothing
                End If

              
    ' Apaga o ficheiro temporário (caso exista)
              
    If tmpFileName <> String.Empty AndAlso _ 
                                                  IO.File.Exists(tmpFileName) Then
                  
    IO.File.Delete(tmpFileName)
                End If
              
    tmpFileName = String.Empty

            Catch ex As Exception : End Try
        End Sub

    End Class

     

    Luego, para imprimir el informe, sólo es necesario:

     

     

    Imports Microsoft.Reporting.WinForms

    Public Class frmMain

        '''<summary>
      
    ''' Imprime o relatório
        '''
    </summary>
      
    Private Sub btnPrint_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles btnPrint.Click

            Try

                Dim
    rpt As New LocalReport
                rpt.ReportPath = Application.StartupPath & "\..\..\rptProducts.rdlc"

              
    ' ---------------------------------------------------------
                ' Definir DataSource, Parameters, etc para o relatório
                ' ---------------------------------------------------------

               
    Using cls As New ReportUtils
                    cls.Print(rpt)
                End Using

            Catch
    ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, _ 
                                        MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End Sub

    End Class

     


    Se aun tienes alguna duda, revise el link:

    http://vbtuga.blogspot.com/2009/07/vbnet-microsoft-reports-imprimir-sem.html

     

    Saludos



    Eduardo Portescheller
    martes, 23 de noviembre de 2010 14:16
    Moderador

Todas las respuestas

  • Hola,

     

    Con que estas tratando de imprimir. Con el printdialog?



    Jason Ulloa
    Mi Blog
    miércoles, 17 de noviembre de 2010 20:03
  • No lo hago a traves del ReportViewer en la barra de herramientas con el boton de imprimir. Se que en el cuadro de dialogo se puede especificar el numero de copias en 2 para que salgan con dos copias, pero quiero que facilite las cosas y no hacerlo manualmente, al contrario automaticamente.
    sábado, 20 de noviembre de 2010 17:12
  • Hola Jonathan, sigue un ejemplo para la impresión, sin utilizar el Microsoft Reports:

    Revisa la parte del cls.Print(rpt)

    Puedes utilizar:

    For I = 1 to 2 (o el numero que quieras de copias)
    cls.Print(rpt)
    Next

    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports Microsoft.Reporting.WinForms

    Public Class ReportUtils
        Implements IDisposable

        Private currentPageIndex As Integer
        Private
    tmpFileName As String = String.Empty
        Private streamList As List(Of Stream)

        '''<summary>
      
    ''' Adiciona a Stream à lista de Streams
        '''
    </summary>
      
    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

            tmpFileName = My.Computer.FileSystem.GetTempFileName()

            Dim s As New FileStream(tmpFileName, FileMode.Create)
            streamList.Add(s)
            Return s

        End Function

      
    '''<summary>
      
    ''' Exporta o ficheiro para uma lista de Streams
        '''
    </summary>
      
    Private Sub ExportToStream(ByVal report As LocalReport)
            Dim deviceInfo As New StringBuilder
            With deviceInfo
                .Append("<DeviceInfo>")
                .Append(" <OutputFormat>EMF</OutputFormat>")
                .Append(" <PageWidth>8.5in</PageWidth>")
                .Append(" <PageHeight>11in</PageHeight>")
                .Append(" <MarginTop>0.25in</MarginTop>")
                .Append(" <MarginLeft>0.25in</MarginLeft>")
                .Append(" <MarginRight>0.25in</MarginRight>")
                .Append(" <MarginBottom>0.25in</MarginBottom>")
                .Append("</DeviceInfo>")
            End With

            Dim
    warnings() As Warning = Nothing
          
    report.Render("Image", deviceInfo.ToString, _
                            AddressOf CreateStream, warnings)

            For Each s As Stream In streamList
                s.Position = 0
            Next
          
    deviceInfo = Nothing

        End Sub

      
    '''<summary>
      
    ''' Quando o PrintDocument está a imprimir, desenha
        ''' a página correspondente, da lista de Streams
        '''
    </summary>
      
    Private Sub PrintPage(ByVal sender As Object, _
                              ByVal ev As PrintPageEventArgs)

            Using pageImage As New Metafile(streamList(currentPageIndex))
                currentPageIndex += 1

                ev.Graphics.DrawImage(pageImage, ev.PageBounds)
                ev.HasMorePages = (currentPageIndex < streamList.Count)
            End Using
        End Sub

      
    '''<summary>
      
    ''' Imprime um relatório sem visualização
        '''
    </summary>
      
    '''<param name="report">Relatório a imprimir</param>
      
    Public Sub Print(ByVal report As LocalReport)

            streamList = New List(Of Stream)

            ' Exporta o ficheiro para uma lista de Streams
          
    Call ExportToStream(report)

            If streamList IsNot Nothing AndAlso streamList.Count > 0 Then

              
    ' Inicia o processo de impressão
              
    Using printDoc As New PrintDocument()

                    If Not printDoc.PrinterSettings.IsValid Then
                        Dim
    msg As String = "Impressora não disponível ou não válida!"
                      
    Throw New ArgumentException(msg)
                    End If

                    AddHandler
    printDoc.PrintPage, AddressOf PrintPage
                    printDoc.Print()
                End Using

            End If

        End Sub

        Public Overloads Sub
    Dispose() Implements IDisposable.Dispose

            Try

              
    ' Fecha as streams e apaga a lista
              
    If streamList IsNot Nothing Then
                    For Each
    s As Stream In streamList
                        s.Close()
                    Next
                  
    streamList.Clear()
                    streamList = Nothing
                End If

              
    ' Apaga o ficheiro temporário (caso exista)
              
    If tmpFileName <> String.Empty AndAlso _ 
                                                  IO.File.Exists(tmpFileName) Then
                  
    IO.File.Delete(tmpFileName)
                End If
              
    tmpFileName = String.Empty

            Catch ex As Exception : End Try
        End Sub

    End Class

     

    Luego, para imprimir el informe, sólo es necesario:

     

     

    Imports Microsoft.Reporting.WinForms

    Public Class frmMain

        '''<summary>
      
    ''' Imprime o relatório
        '''
    </summary>
      
    Private Sub btnPrint_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles btnPrint.Click

            Try

                Dim
    rpt As New LocalReport
                rpt.ReportPath = Application.StartupPath & "\..\..\rptProducts.rdlc"

              
    ' ---------------------------------------------------------
                ' Definir DataSource, Parameters, etc para o relatório
                ' ---------------------------------------------------------

               
    Using cls As New ReportUtils
                    cls.Print(rpt)
                End Using

            Catch
    ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, _ 
                                        MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End Sub

    End Class

     


    Se aun tienes alguna duda, revise el link:

    http://vbtuga.blogspot.com/2009/07/vbnet-microsoft-reports-imprimir-sem.html

     

    Saludos



    Eduardo Portescheller
    martes, 23 de noviembre de 2010 14:16
    Moderador