Foros del Web » Programación para mayores de 30 ;) » .NET »

Imprimir un linea

Estas en el tema de Imprimir un linea en el foro de .NET en Foros del Web. Hola a todos¡¡ Lo que quiero hacer es que mi aplicacion imprima una linea cuando se cumpla una condicion,pero que no saque papel la impresora ...
  #1 (permalink)  
Antiguo 24/01/2007, 03:26
 
Fecha de Ingreso: diciembre-2006
Mensajes: 30
Antigüedad: 17 años, 4 meses
Puntos: 0
Imprimir un linea

Hola a todos¡¡
Lo que quiero hacer es que mi aplicacion imprima una linea cuando se cumpla una condicion,pero que no saque papel la impresora cuando termine,en fin lo que quiero es que se vayan imprimiendo lineas segun se vaya cumpliendo la condicion y que termine en numero determinado de lineas.
No se si me he explicado bien.
Agradecería cualquier tipo de orientacion.
Muchas gracias
  #2 (permalink)  
Antiguo 24/01/2007, 03:31
Avatar de Korku  
Fecha de Ingreso: noviembre-2003
Ubicación: En un lugar oscuro...
Mensajes: 688
Antigüedad: 20 años, 5 meses
Puntos: 5
Re: Imprimir un linea

Cita:
Iniciado por Naika_L Ver Mensaje
Hola a todos¡¡
Lo que quiero hacer es que mi aplicacion imprima una linea cuando se cumpla una condicion,pero que no saque papel la impresora cuando termine,en fin lo que quiero es que se vayan imprimiendo lineas segun se vaya cumpliendo la condicion y que termine en numero determinado de lineas.
No se si me he explicado bien.
Agradecería cualquier tipo de orientacion.
Muchas gracias
Hola,

¿Qué usas para imprimir? ¿Objeto Printer?

Saludos
__________________
Carpe diem quam minimum credula postero.


http://www.programador-freelance.es
  #3 (permalink)  
Antiguo 24/01/2007, 11:01
 
Fecha de Ingreso: diciembre-2006
Mensajes: 30
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Imprimir un linea

a que libreria pertenece ese objeto??
si lo coloco directamente en el compilador no me lo reconoce.
Estoy probando con la clase printDocument y el metodo print pero por ahora no he sacado nada en claro.
  #4 (permalink)  
Antiguo 24/01/2007, 11:36
Avatar de Korku  
Fecha de Ingreso: noviembre-2003
Ubicación: En un lugar oscuro...
Mensajes: 688
Antigüedad: 20 años, 5 meses
Puntos: 5
Re: Imprimir un linea

Te paso varios artículos sobre el objeto PRINT:

http://www.vbdotnetheaven.com/Sections/Printing.asp
http://www.startvbdotnet.com/controls/printdialog.aspx

Saludos :)
__________________
Carpe diem quam minimum credula postero.


http://www.programador-freelance.es
  #5 (permalink)  
Antiguo 06/02/2007, 05:07
 
Fecha de Ingreso: diciembre-2006
Mensajes: 30
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Imprimir un linea

Muchas gracias, me sirvio de mucha ayuda los links.
Ahora me ga surgido otro problema, a ver si me puedes ayudar.
Utilizo un objeto printdocument para imprimir,lo que sucede es que el metodo print no imprime dinamicamente,es decir, si hay que imprimir 3 lineas en vez de imprimir la linea cada vez que llamo a el metodo,lo que hace es esperar para imprimir las 3 a la vez y sacar la pagina.
A mi lo que me gustaria es imprimir linea por linea pero dinámicamene,imprimir la linea directamente en la impresora, y que no acabase la hoja hasta que yo le indicase fin de hoja.
He estado mirando los metodos BeginPrint y EndPrint, pero no estoy segura, si podrías orientarme nuevamente te lo agradecería.

Muchas gracias :) y saludos
  #6 (permalink)  
Antiguo 08/02/2007, 09:57
 
Fecha de Ingreso: diciembre-2006
Mensajes: 30
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Imprimir un linea

Ya descubri como hacerlo, por si ha alguien le sirve aqui es donde he encontrado mi solucion:

Imports System.IO
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices

Public Class Form1
Inherits System.Windows.Forms.Form


' Click event handler for a button - designed to show how to use the
' SendFileToPrinter and SendBytesToPrinter functions.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Allow the user to select a file.
Dim ofd As New OpenFileDialog
If ofd.ShowDialog(Me) Then
' Allow the user to select a printer.
Dim pd As New PrintDialog
pd.PrinterSettings = New PrinterSettings
If (pd.ShowDialog() = DialogResult.OK) Then
' Print the file to the printer.
RawPrinterHelper.SendFileToPrinter(pd.PrinterSetti ngs.PrinterName, ofd.FileName)
End If
End If
End Sub ' Button1_Click()

' Click event handler for a button - designed to show how to use the
' SendBytesToPrinter function to send a string to the printer.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim s As String
Dim pd As New PrintDialog
Dim cont As Integer = 0
Dim o As Integer = 0
Dim array(10) As String
' You need a string to send.
s = "Hello, this is a test"
' Open the printer dialog box, and then allow the user to select a printer.
pd.PrinterSettings = New PrinterSettings
If (pd.ShowDialog() = DialogResult.OK) Then
While cont <> 10
array(cont) = "hola holita " & cont
cont = cont + 1
End While
o = 0

s = Chr(10) & array(o) + vbNewLine + array(o + 1) + vbNewLine + array(o + 2) + vbNewLine + array(o + 3) + vbNewLine + array(o + 4) + vbNewLine + array(o + 5) '& Chr(16)
RawPrinterHelper.SendStringToPrinter(pd.PrinterSet tings.PrinterName, s)


End If
End Sub ' Button2_Click()
End Class
Public Class RawPrinterHelper
' Structure and API declarions:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Structure DOCINFOW
<MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
<MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
<MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
End Structure

<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
End Function

' SendBytesToPrinter()
' When the function is given a printer name and an unmanaged array of
' bytes, the function sends those bytes to the print queue.
' Returns True on success or False on failure.
Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
Dim hPrinter As IntPtr ' The printer handle.
Dim dwError As Int32 ' Last error - in case there was trouble.
Dim di As DOCINFOW ' Describes your document (name, port, data type).
Dim dwWritten As Int32 ' The number of bytes written by WritePrinter().
Dim bSuccess As Boolean ' Your success code.

' Set up the DOCINFO structure.
With di
.pDocName = "My Visual Basic .NET RAW Document"
.pDataType = "RAW"
End With
' Assume failure unless you specifically succeed.
bSuccess = False
If OpenPrinter(szPrinterName, hPrinter, 0) Then
If StartDocPrinter(hPrinter, 1, di) Then
If StartPagePrinter(hPrinter) Then
' Write your printer-specific bytes to the printer.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
EndPagePrinter(hPrinter)
End If
EndDocPrinter(hPrinter)
End If
ClosePrinter(hPrinter)
End If
' If you did not succeed, GetLastError may give more information
' about why not.
If bSuccess = False Then
dwError = Marshal.GetLastWin32Error()
End If
Return bSuccess
End Function ' SendBytesToPrinter()

' SendFileToPrinter()
' When the function is given a file name and a printer name,
' the function reads the contents of the file and sends the
' contents to the printer.
' Presumes that the file contains printer-ready data.
' Shows how to use the SendBytesToPrinter function.
' Returns True on success or False on failure.
Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
' Open the file.
Dim fs As New FileStream(szFileName, FileMode.Open)
' Create a BinaryReader on the file.
Dim br As New BinaryReader(fs)
' Dim an array of bytes large enough to hold the file's contents.
Dim bytes(fs.Length) As Byte
Dim bSuccess As Boolean
' Your unmanaged pointer.
Dim pUnmanagedBytes As IntPtr

' Read the contents of the file into the array.
bytes = br.ReadBytes(fs.Length)
' Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(fs.Length)
' Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, fs.Length)
' Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
' Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes)
Return bSuccess
End Function ' SendFileToPrinter()

' When the function is given a string and a printer name,
' the function sends the string to the printer as raw bytes.
Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String)
Dim pBytes As IntPtr
Dim dwCount As Int32
' How many characters are in the string?
dwCount = szString.Length()
' Assume that the printer is expecting ANSI text, and then convert
' the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString)
' Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount)
Marshal.FreeCoTaskMem(pBytes)
End Function
End Class
  #7 (permalink)  
Antiguo 09/02/2007, 03:44
Avatar de Korku  
Fecha de Ingreso: noviembre-2003
Ubicación: En un lugar oscuro...
Mensajes: 688
Antigüedad: 20 años, 5 meses
Puntos: 5
Re: Imprimir un linea

Perfecto, lamento no contestarte a tiempo

Saludos
__________________
Carpe diem quam minimum credula postero.


http://www.programador-freelance.es
  #8 (permalink)  
Antiguo 22/09/2010, 11:03
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Imprimir un linea

alguien me puede ayudar con un tema parecido ,
Quiero imprimir fecha hora y lote en un folio segun le diga yo que lo haga ,
imprimiendo tantos lotes como le diga en el mismo folio sin sacarlo puede ser?
  #9 (permalink)  
Antiguo 02/03/2011, 06:55
Usuario no validado
 
Fecha de Ingreso: mayo-2005
Ubicación: Benicàssim
Mensajes: 56
Antigüedad: 19 años
Puntos: 0
Respuesta: Re: Imprimir un linea

Una pregunta Nika_L:

Mi analizador de Visual Studio 2010 no me permite ejecutar la resolucion que pusiste. Me da una advertencia en

Detalle ERROR: La variable 'di' se ha pasado como referencia antes de haberle asignado un valor. Podría producirse una excepción de referencia nula en tiempo de ejecución. Asegúrese de que la estructura o todos los miembros de referencia se hayan inicializado antes de que se utilicen.

Código vb:
Ver original
  1. If OpenPrinter(szPrinterName, hPrinter, 0) Then
  2.             If StartDocPrinter(hPrinter, 1, di) Then  [B][COLOR="Red"][U] <-- AQUI el ERROR [/U][/COLOR][/B]
  3.  
  4. Error
  5.  
  6.                 If StartPagePrinter(hPrinter) Then
  7.                     ' Write your printer-specific bytes to the printer.
  8.                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
  9.                     EndPagePrinter(hPrinter)
  10.                 End If
  11.                 EndDocPrinter(hPrinter)
  12.             End If
  13.             ClosePrinter(hPrinter)
  14.         End If

¿Podrías darme una mano con esto?
Gracias!
  #10 (permalink)  
Antiguo 02/03/2011, 07:16
Avatar de Aquaventus  
Fecha de Ingreso: junio-2010
Ubicación: Lima-Peru , En el alba de la naturaleza
Mensajes: 2.105
Antigüedad: 13 años, 10 meses
Puntos: 267
Respuesta: Imprimir un linea

Te has dado cuenta que este post es de hace 4 años?, no creo que ese usuario al que preguntas te responda... xq no se ah conectado desde octurbe del 2007. Create un post nuevo y consulta tus dudas. Saludos!
__________________
Internet es tener todo el conocimiento global a tu disposición.
Desarrollo de Software - Ejemplos .Net
  #11 (permalink)  
Antiguo 02/03/2011, 09:36
Usuario no validado
 
Fecha de Ingreso: mayo-2005
Ubicación: Benicàssim
Mensajes: 56
Antigüedad: 19 años
Puntos: 0
Respuesta: Imprimir un linea

No me percaté en eso, muchas gracias!
  #12 (permalink)  
Antiguo 20/06/2011, 10:06
 
Fecha de Ingreso: mayo-2011
Mensajes: 3
Antigüedad: 13 años
Puntos: 0
Respuesta: Imprimir un linea

Por si a alguien le sirve, les comento que la funcion esta correcta solo consideren lo siguiente para quitar los warning en vb 2010:

Donde se declara la variable di haganlo de la siguiente forma:
Dim di As New DOCINFOW()

Y al final de la funcion SendStringToPrinter agregen un:
Return True

Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:59.