Advertisement
Fhernd

Principal.cs

Mar 21st, 2018
1,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. using System.Windows.Forms;
  5.  
  6. namespace R816ImprimirBloqueTexto
  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 texto = "La programación reactiva produce aplicaciones más robustas, eficientes y más amigables: " +
  18.                            "Arquitectura orientada a evento, Resistencia a fallos, Garantía de respuesta, Escalabilidad.";
  19.  
  20.             PrintDocument documento = new DocumentoParrafo(texto);
  21.             documento.PrintPage += documento_PrintPage;
  22.  
  23.             PrintDialog printDialog = new PrintDialog();
  24.             printDialog.Document = documento;
  25.  
  26.             if (printDialog.ShowDialog() == DialogResult.OK)
  27.             {
  28.                 documento.Print();
  29.             }
  30.         }
  31.  
  32.         private void documento_PrintPage(object sender, PrintPageEventArgs e)
  33.         {
  34.             DocumentoParrafo documento = (DocumentoParrafo) sender;
  35.  
  36.             using (Font fuente = new Font("Trebuchet", 15))
  37.             {
  38.                 e.Graphics.DrawString(documento.Texto, fuente, Brushes.Black, e.MarginBounds, StringFormat.GenericDefault);
  39.             }
  40.         }
  41.     }
  42.  
  43.     public class DocumentoParrafo : PrintDocument
  44.     {
  45.         private string texto;
  46.  
  47.         public string Texto { get; set; }
  48.  
  49.         public DocumentoParrafo(string texto)
  50.         {
  51.             this.Texto = texto;
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement