diciembre 11, 2006

Imprimir directamente con ASP.NET


1. En el evento Load cargo la lista de impresoras disponibles localemente.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Me.DropDownListImpresoras.DataSource = PrinterSettings.InstalledPrinters()
Me.DropDownListImpresoras.DataBind()
Me.DropDownListImpresoras.SelectedIndex = 0
End If
End Sub

2. Al hacer clic en el botón imprimir
Protected Sub ButtonImprimir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonImprimir.Click
'Create an instance of PrintDocument
Dim printdoc As New System.Drawing.Printing.PrintDocument()
' Set the printer name
printdoc.PrinterSettings.PrinterName = Me.DropDownListImpresoras.Text
' Handle printing
AddHandler printdoc.PrintPage, AddressOf Me.printdoc_PrintPage
' Print!
printdoc.Print()
End Sub

3. Evento manejado al imprimir que es en donde se especifica lo que se desea imprimir.

Private Sub printdoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'Definición de la fte a utilizar.
Dim printFont As System.Drawing.Font = New System.Drawing.Font("Courier New", 12)

' Dibujar el nombre escrito y que se desea imprimir directamente.
e.Graphics.DrawString(Me.TextBox1.Text, printFont, System.Drawing.Brushes.Black, New System.Drawing.PointF(10.0F, 50.0F))
End Sub

No hay comentarios.: