Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Impresion directa a impresora por defecto

478 views
Skip to first unread message

Baldor

unread,
Apr 18, 2006, 5:22:54 AM4/18/06
to
Pregunta sencilla (creo).

Cuál es el equivalente en .NET al antiguo:

Printer.Print ("Texto a imprimir directamente a la impresora")
Printer.EndDoc


Rubén Vigón

unread,
Apr 18, 2006, 5:35:38 AM4/18/06
to
Hola Baldor,

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

Baldor

unread,
Apr 18, 2006, 5:55:46 AM4/18/06
to
Gracias por la respuesta Ruben.
Aunque el comportamiento es algo diferente, por ejemplo, con Printer.Print,
se salta de linea automaticamente, de forma que la siguiente instruccion
Print comienza a imprimir en la siguiente linea. Y con Printer.Print("")
dejaría una linea en blanco.
En tu ejemplo siempre imprimiría en la esquina superior izquierda de la
hoja.

ż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?


Rubén Vigón

unread,
Apr 18, 2006, 6:04:20 AM4/18/06
to
Hola de nuevo, Baldor

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)
[···]

Baldor

unread,
Apr 18, 2006, 6:38:36 AM4/18/06
to
Ok, gracias, lo hare de ese modo.
Es un poco tedioso pero funciona a la perfección.


Bard...@gmail.com

unread,
Apr 20, 2006, 4:28:55 AM4/20/06
to
He aquí otra forma, la cual me funciona muy bien, atraves de los
objetos manejadores de impresión. Saludos,
Carlos Bardales

'----------------------------------------------------------------------------------------------------------------------
' 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

rai240...@gmail.com

unread,
Apr 29, 2015, 9:10:26 AM4/29/15
to


yo utilizo lo mismo que comento ruben, me funciona asi puedes manejar a tu antojo el diseño de tu documento .....ahopra bien sumo una pregunta es posible que ese evento del printdocument poder guardarlo ? es decir cuando se genera la vista previa lo podamos guardar en un archivo? para luyego pasarlo a pdf???
0 new messages