Advertisement
Fhernd

Principal.cs

Mar 22nd, 2018
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. using System.Windows.Forms;
  5.  
  6. namespace R817VistaPreviaImpresion
  7. {
  8.     public partial class Principal : Form
  9.     {
  10.         private PrintDocument documento;
  11.  
  12.         public Principal()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         private void btnMostrarVistaPrevia_Click(object sender, EventArgs e)
  18.         {
  19.             ppcVistaPrevia.Zoom = Single.Parse(lstZoom.Text) / 100;
  20.  
  21.             ppcVistaPrevia.Rows = 2;
  22.  
  23.             ppcVistaPrevia.Document = documento;
  24.         }
  25.  
  26.         private void Principal_Load(object sender, EventArgs e)
  27.         {
  28.             for (int i = 1; i <= 10; i++)
  29.             {
  30.                 lstZoom.Items.Add((i * 10).ToString());
  31.             }
  32.  
  33.             string[] texto = new string[100];
  34.             for (int i = 0; i < 100; i++)
  35.             {
  36.                 texto[i] = i.ToString();
  37.                 texto[i] += ": C# es un lenguaje de programación multi-paradigma.";
  38.             }
  39.  
  40.             documento = new DocumentoTexto(texto);
  41.             documento.PrintPage += this.documento_PrintPage;
  42.  
  43.             lstZoom.Text = "100";
  44.             ppcVistaPrevia.Zoom = 1;
  45.             ppcVistaPrevia.Document = documento;
  46.             ppcVistaPrevia.Rows = 2;
  47.         }
  48.  
  49.         private void documento_PrintPage(object sender, PrintPageEventArgs e)
  50.         {
  51.             DocumentoTexto doc = (DocumentoTexto)sender;
  52.  
  53.             using (Font font = new Font("Trebuchet", 10))
  54.             {
  55.                 float altoLinea = font.GetHeight(e.Graphics);
  56.  
  57.                 float x = e.MarginBounds.Left;
  58.                 float y = e.MarginBounds.Top;
  59.  
  60.                 doc.NumeroPagina += 1;
  61.  
  62.                 while ((y + altoLinea) < e.MarginBounds.Bottom && doc.Desplazamiento <= doc.Texto.GetUpperBound(0))
  63.                 {
  64.                     e.Graphics.DrawString(doc.Texto[doc.Desplazamiento], font,
  65.                         Brushes.Black, x, y);
  66.                    
  67.                     doc.Desplazamiento += 1;
  68.                     y += altoLinea;
  69.                 }
  70.  
  71.                 if (doc.Desplazamiento < doc.Texto.GetUpperBound(0))
  72.                 {
  73.                     e.HasMorePages = true;
  74.                 }
  75.                 else
  76.                 {
  77.                     doc.Desplazamiento = 0;
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     class DocumentoTexto : PrintDocument
  84.     {
  85.         private string[] texto;
  86.         private int numeroPagina;
  87.         private int desplazamiento;
  88.  
  89.         public string[] Texto
  90.         {
  91.             get { return texto; }
  92.             set { texto = value; }
  93.         }
  94.  
  95.         public int NumeroPagina
  96.         {
  97.             get { return numeroPagina; }
  98.  
  99.             set { numeroPagina = value; }
  100.         }
  101.  
  102.         public int Desplazamiento
  103.         {
  104.             get { return desplazamiento; }
  105.             set { desplazamiento = value; }
  106.         }
  107.  
  108.         public DocumentoTexto(string[] texto)
  109.         {
  110.             this.Texto = texto;
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement