Cuál es el equivalente en .NET al antiguo:
Printer.Print ("Texto a imprimir directamente a la impresora")
Printer.EndDoc
Una manera sencilla es usar un objeto «System.Drawing.Printing.PrintDocument» y su evento «PrintPage»; por ejemplo:
Dim Pd As New Printing.PrintDocument
AddHandler Pd.PrintPage, AddressOf ImprimirPagina
Pd.Print()
Private Sub ImprimirPagina(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)
e.Graphics.DrawString("Texto a imprimir directamente a la impresora", _
New Font("Arial", 10), Brushes.Black, _
e.MarginBounds.Left, e.MarginBounds.Top)
e.HasMorePages = False
End Sub
Un saludo!
Rubén Vigón
Microsoft MVP Visual Basic
http://vigon.mvps.org
żNo existe algo que se comporte igual que el Printer.Print?
O mas claramente, żcomo imprimo un listado muy sencillo (de una pocas
lineas) sin necesidad de usar reports?
Que yo sepa, es necesario controlar por código las coordenadas horizontal y vertical del texto a imprimir. Es decir, continuando con el ejemplillo anterior:
Private Sub ImprimirPagina(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)
Dim y As Single = e.MarginBounds.Top, f As New Font("Arial", 10)
e.Graphics.DrawString("Primera línea", f, Brushes.Black, e.MarginBounds.Left, y)
y += f.GetHeight(e.Graphics)
e.Graphics.DrawString("Segunda línea", f, Brushes.Black, e.MarginBounds.Left, y)
y += f.GetHeight(e.Graphics)
e.Graphics.DrawString("Tercera línea", f, Brushes.Black, e.MarginBounds.Left, y)
y += f.GetHeight(e.Graphics)
[···]
'----------------------------------------------------------------------------------------------------------------------
' IMPRIMIR UN ARCHIVO DE TEXTO EN LA IMPRESORA POR DEFECTO O
' MOSTRANDO LA LISTA DE IMPRESORAS, OPCIONAL VISTA PREVIA
'----------------------------------------------------------------------------------------------------------------------
Public Shared Sub AsciiToPrinter(ByVal Archivo As String, Optional
ByVal
PageProperties As PageSettings = Nothing, _
Optional ByVal showPrinters As Boolean = True, _
Optional ByVal ShowPreview As Boolean = True)
Try
If Not My.Computer.FileSystem.FileExists(Archivo) Then
MessageBox.Show("No existe el archivo:" & Archivo)
Exit Sub
End If
Dim streamToPrint As IO.StreamReader = New
IO.StreamReader(Archivo)
Dim pd As TextPrintDocument = New
TextPrintDocument(streamToPrint)
Try
If Not PageProperties Is Nothing Then
pd.DefaultPageSettings = PageProperties
End If
If showPrinters Then
Dim dlg As New PrintDialog()
dlg.Document = pd
Dim result As DialogResult = dlg.ShowDialog()
If (result = System.Windows.Forms.DialogResult.OK)
Then
If Not ShowPreview Then
pd.Print()
Else
Dim dlg2 As New PrintPreviewDialog()
dlg2.Document = pd
dlg2.ShowDialog()
End If
End If
Else
If Not ShowPreview Then
pd.Print()
Else
Dim dlg2 As New PrintPreviewDialog()
dlg2.Document = pd
dlg2.ShowDialog()
End If
End If
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'---------------------------------------------------------------------------
'CLASE PrintDocument PARA IMPRIMIR ARCHIVOS DE TEXTO.
'---------------------------------------------------------------------------
Public Class TextPrintDocument
Inherits PrintDocument
Private printFont As Font
Private streamToPrint As IO.StreamReader
Public Sub New(ByVal streamToPrint As IO.StreamReader)
MyBase.New()
Me.streamToPrint = streamToPrint
End Sub
Protected Overrides Sub OnBeginPrint(ByVal ev As PrintEventArgs)
MyBase.OnBeginPrint(ev)
printFont = New Font("Arial", 10)
End Sub
Protected Overrides Sub OnPrintPage(ByVal ev As PrintPageEventArgs)
MyBase.OnPrintPage(ev)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, _
yPos, New StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
'--------------------------------------------------------
' RETORNA EL NOMBRE DE LA IMPRESORA POR DEFECTO
'-------------------------------------------------------
Public Shared Function DefaultPrinter() As String
Dim pd As New System.Drawing.Printing.PrintDocument
Return pd.PrinterSettings.PrinterName
End Function