SoloCodigo

CLR: .Net / Mono / Boo / Otros CLR => C# => Mensaje iniciado por: tavOni en Miércoles 18 de Abril de 2007, 05:09

Título: Imprimir Desde C#
Publicado por: tavOni en Miércoles 18 de Abril de 2007, 05:09
Hola quisiera saber como puedo hacer que al dar click en una form
se pueda mandar imprimir una realizada con los datos que obtengo
en ella
Título: Re: Imprimir Desde C#
Publicado por: Neopro en Miércoles 18 de Abril de 2007, 18:00
Puedes imprimir a través del servicio de Windows:

Código: Text
  1. using System.Drawing;
  2. using System.Drawing.Printing;
  3.  

Código: Text
  1. public static PrintDocument Imprimir = new PrintDocument();
  2.  

Código: Text
  1.       private static void Imprimir_PrintPage(Object sender, PrintPageEventArgs e)
  2.         {
  3.             Font fuente_de_texto = new Font("Courier New", 12);
  4.             e.Graphics.DrawString(texto_a_imprimir, fuente_de_texto, Brushes.Black, 0, 0);
  5.         }
  6.  

Debes pasarle como variable imp el texto en modo STRING para que lo imprima. Si necesitas un salto de linea agrega al texto de la siguiente manera:

Código: Text
  1. imp = imp + "\n";
  2.  

Código: Text
  1.         public static void imprime(string imp)
  2.         {
  3.             try
  4.             {
  5.                 texto_a_imprimir = imp;
  6.                 Imprimir.PrintPage += new PrintPageEventHandler(Imprimir_PrintPage);
  7.                 Imprimir.Print();
  8.             }
  9.             catch (Exception e)
  10.             {
  11.                 MessageBox.Show("Error." + e, "Error");
  12.             }
  13.         }
  14.  

Debes obtener el texto de alguna parte, junta los datos que obtienes en el form, en un solo STRING.