Advertisement
Fhernd

Principal.cs

Mar 21st, 2018
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. using System.Windows.Forms;
  5.  
  6. namespace R815ImprimirVariosDocumentos
  7. {
  8.     public partial class Principal : Form
  9.     {
  10.         public Principal()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.         private void btnImprimir_Click(object sender, EventArgs e)
  16.         {
  17.             string[] textoImpresion = new string[101];
  18.             for (int i = 0; i < 101; i++)
  19.             {
  20.                 textoImpresion[i] = i.ToString();
  21.                 textoImpresion[i] += ": Programad como un profesional.";
  22.             }
  23.  
  24.             PrintDocument documento = new DocumentoTexto(textoImpresion);
  25.             documento.PrintPage += documento_PrintPage;
  26.  
  27.             PrintDialog printDialog = new PrintDialog();
  28.             printDialog.Document = documento;
  29.  
  30.             if (printDialog.ShowDialog() == DialogResult.OK)
  31.             {
  32.                 documento.Print();
  33.             }
  34.         }
  35.  
  36.         private void documento_PrintPage(object sender, PrintPageEventArgs e)
  37.         {
  38.             DocumentoTexto doc = (DocumentoTexto) sender;
  39.  
  40.             using (Font fuente = new Font("Trebuchet", 10))
  41.             {
  42.                 float altoLinea = fuente.GetHeight(e.Graphics);
  43.  
  44.                 float x = e.MarginBounds.Left;
  45.                 float y = e.MarginBounds.Top;
  46.  
  47.                 doc.NumeroPagina += 1;
  48.  
  49.                 while ((y + altoLinea) < e.MarginBounds.Bottom && doc.Desplazamiento <= doc.Texto.GetUpperBound(0))
  50.                 {
  51.                     e.Graphics.DrawString(doc.Texto[doc.Desplazamiento], fuente, Brushes.Black, x, y);
  52.  
  53.                     doc.Desplazamiento += 1;
  54.  
  55.                     y += altoLinea;
  56.                 }
  57.  
  58.                 if (doc.Desplazamiento < doc.Texto.GetUpperBound(0))
  59.                 {
  60.                     e.HasMorePages = true;
  61.                 }
  62.                 else
  63.                 {
  64.                     doc.Desplazamiento = 0;
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement