Created
March 22, 2018 12:54
-
-
Save Fhernd/61b03f6f29ba6aa42c0854b948d5ee13 to your computer and use it in GitHub Desktop.
Vista previa documento. OrtizOL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Printing; | |
using System.Windows.Forms; | |
namespace R817VistaPreviaImpresion | |
{ | |
public partial class Principal : Form | |
{ | |
private PrintDocument documento; | |
public Principal() | |
{ | |
InitializeComponent(); | |
} | |
private void btnMostrarVistaPrevia_Click(object sender, EventArgs e) | |
{ | |
ppcVistaPrevia.Zoom = Single.Parse(lstZoom.Text) / 100; | |
ppcVistaPrevia.Rows = 2; | |
ppcVistaPrevia.Document = documento; | |
} | |
private void Principal_Load(object sender, EventArgs e) | |
{ | |
for (int i = 1; i <= 10; i++) | |
{ | |
lstZoom.Items.Add((i * 10).ToString()); | |
} | |
string[] texto = new string[100]; | |
for (int i = 0; i < 100; i++) | |
{ | |
texto[i] = i.ToString(); | |
texto[i] += ": C# es un lenguaje de programación multi-paradigma."; | |
} | |
documento = new DocumentoTexto(texto); | |
documento.PrintPage += this.documento_PrintPage; | |
lstZoom.Text = "100"; | |
ppcVistaPrevia.Zoom = 1; | |
ppcVistaPrevia.Document = documento; | |
ppcVistaPrevia.Rows = 2; | |
} | |
private void documento_PrintPage(object sender, PrintPageEventArgs e) | |
{ | |
DocumentoTexto doc = (DocumentoTexto)sender; | |
using (Font font = new Font("Trebuchet", 10)) | |
{ | |
float altoLinea = font.GetHeight(e.Graphics); | |
float x = e.MarginBounds.Left; | |
float y = e.MarginBounds.Top; | |
doc.NumeroPagina += 1; | |
while ((y + altoLinea) < e.MarginBounds.Bottom && doc.Desplazamiento <= doc.Texto.GetUpperBound(0)) | |
{ | |
e.Graphics.DrawString(doc.Texto[doc.Desplazamiento], font, | |
Brushes.Black, x, y); | |
doc.Desplazamiento += 1; | |
y += altoLinea; | |
} | |
if (doc.Desplazamiento < doc.Texto.GetUpperBound(0)) | |
{ | |
e.HasMorePages = true; | |
} | |
else | |
{ | |
doc.Desplazamiento = 0; | |
} | |
} | |
} | |
} | |
class DocumentoTexto : PrintDocument | |
{ | |
private string[] texto; | |
private int numeroPagina; | |
private int desplazamiento; | |
public string[] Texto | |
{ | |
get { return texto; } | |
set { texto = value; } | |
} | |
public int NumeroPagina | |
{ | |
get { return numeroPagina; } | |
set { numeroPagina = value; } | |
} | |
public int Desplazamiento | |
{ | |
get { return desplazamiento; } | |
set { desplazamiento = value; } | |
} | |
public DocumentoTexto(string[] texto) | |
{ | |
this.Texto = texto; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment