• Viernes 29 de Marzo de 2024, 10:55

Autor Tema:  Imprimir Archivo de Texto  (Leído 5636 veces)

JaviMarciano

  • Miembro activo
  • **
  • Mensajes: 97
    • Ver Perfil
Imprimir Archivo de Texto
« en: Lunes 12 de Abril de 2010, 18:07 »
0
Como se imprime un archivo de texto en c#

Jeysscarr

  • Miembro MUY activo
  • ***
  • Mensajes: 134
  • Nacionalidad: co
    • Ver Perfil
Re: Imprimir Archivo de Texto
« Respuesta #1 en: Lunes 12 de Abril de 2010, 18:45 »
0
Yo lo hice de esta forma para archivos de varias páginas:
Suponiendo que tienes un boton que hace el trabajo que se llama PrintButton

Código: C#
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6.  
  7. namespace PrintApp
  8. {
  9.     public class Form1 : Form
  10.     {
  11.    
  12.         private PrintDocument printDocument1 = new PrintDocument();
  13.         private string stringToPrint;
  14.        
  15.  
  16.         private void LeerArchivo()
  17.         {
  18.             string docName = "archivo.txt";
  19.             string docPath = @"c:";
  20.             printDocument1.DocumentName = docName;
  21.             using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
  22.             using (StreamReader reader = new StreamReader(stream))
  23.             {
  24.                 stringToPrint = reader.ReadToEnd();
  25.             }
  26.         }
  27.  
  28.         private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  29.         {
  30.             int charactersOnPage = 0;
  31.             int linesPerPage = 0;
  32.  
  33.          
  34.             e.Graphics.MeasureString(stringToPrint, this.Font,
  35.                 e.MarginBounds.Size, StringFormat.GenericTypographic,
  36.                 out charactersOnPage, out linesPerPage);
  37.  
  38.          
  39.             e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
  40.                 e.MarginBounds, StringFormat.GenericTypographic);
  41.  
  42.            
  43.             stringToPrint = stringToPrint.Substring(charactersOnPage);
  44.  
  45.             e.HasMorePages = (stringToPrint.Length > 0);
  46.         }
  47.  
  48.         private void printButton_Click(object sender, EventArgs e)
  49.         {
  50.             LeerArchivo();
  51.             printDocument1.Print();
  52.         }
  53.  
  54.         [STAThread]
  55.         static void Main()
  56.         {
  57.             Application.EnableVisualStyles();
  58.             Application.SetCompatibleTextRenderingDefault(false);
  59.             Application.Run(new Form1());
  60.         }
  61.     }
  62. }
  63.  
  64.  

La funcion LeerArchivo hace lo necesario para leer los caracteres y dibujarlos en una "Hoja en blanco" del printdocument--- despues este manda a imprimir la o las hojas... para probar sin impresora... usa un preview
Nuestro poder está creciendo mas rápido que nuestra sabiduria

JaviMarciano

  • Miembro activo
  • **
  • Mensajes: 97
    • Ver Perfil
Re: Imprimir Archivo de Texto
« Respuesta #2 en: Lunes 12 de Abril de 2010, 20:32 »
0
Cita de: "Jeysscarr"
Yo lo hice de esta forma para archivos de varias páginas:
Suponiendo que tienes un boton que hace el trabajo que se llama PrintButton

Código: C#
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6.  
  7. namespace PrintApp
  8. {
  9.     public class Form1 : Form
  10.     {
  11.    
  12.         private PrintDocument printDocument1 = new PrintDocument();
  13.         private string stringToPrint;
  14.        
  15.  
  16.         private void LeerArchivo()
  17.         {
  18.             string docName = "archivo.txt";
  19.             string docPath = @"c:";
  20.             printDocument1.DocumentName = docName;
  21.             using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
  22.             using (StreamReader reader = new StreamReader(stream))
  23.             {
  24.                 stringToPrint = reader.ReadToEnd();
  25.             }
  26.         }
  27.  
  28.         private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  29.         {
  30.             int charactersOnPage = 0;
  31.             int linesPerPage = 0;
  32.  
  33.          
  34.             e.Graphics.MeasureString(stringToPrint, this.Font,
  35.                 e.MarginBounds.Size, StringFormat.GenericTypographic,
  36.                 out charactersOnPage, out linesPerPage);
  37.  
  38.          
  39.             e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
  40.                 e.MarginBounds, StringFormat.GenericTypographic);
  41.  
  42.            
  43.             stringToPrint = stringToPrint.Substring(charactersOnPage);
  44.  
  45.             e.HasMorePages = (stringToPrint.Length > 0);
  46.         }
  47.  
  48.         private void printButton_Click(object sender, EventArgs e)
  49.         {
  50.             LeerArchivo();
  51.             printDocument1.Print();
  52.         }
  53.  
  54.         [STAThread]
  55.         static void Main()
  56.         {
  57.             Application.EnableVisualStyles();
  58.             Application.SetCompatibleTextRenderingDefault(false);
  59.             Application.Run(new Form1());
  60.         }
  61.     }
  62. }
  63.  
  64.  

La funcion LeerArchivo hace lo necesario para leer los caracteres y dibujarlos en una "Hoja en blanco" del printdocument--- despues este manda a imprimir la o las hojas... para probar sin impresora... usa un preview

buenisimo muchas gracias

JaviMarciano

  • Miembro activo
  • **
  • Mensajes: 97
    • Ver Perfil
Re: Imprimir Archivo de Texto
« Respuesta #3 en: Lunes 12 de Abril de 2010, 22:51 »
0
otra duda no se puede enviar un archivo de una o sea no agregando linea por linea
porq el archivo que se imprime no tiene el mismo formato que el archivo que tengo almacenado