Created
March 21, 2018 12:45
-
-
Save Fhernd/0ff4fa92917affb336030d9546aee05b to your computer and use it in GitHub Desktop.
Impresión de un documento de múltiples páginas. 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 R815ImprimirVariosDocumentos | |
{ | |
public partial class Principal : Form | |
{ | |
public Principal() | |
{ | |
InitializeComponent(); | |
} | |
private void btnImprimir_Click(object sender, EventArgs e) | |
{ | |
string[] textoImpresion = new string[101]; | |
for (int i = 0; i < 101; i++) | |
{ | |
textoImpresion[i] = i.ToString(); | |
textoImpresion[i] += ": Programad como un profesional."; | |
} | |
PrintDocument documento = new DocumentoTexto(textoImpresion); | |
documento.PrintPage += documento_PrintPage; | |
PrintDialog printDialog = new PrintDialog(); | |
printDialog.Document = documento; | |
if (printDialog.ShowDialog() == DialogResult.OK) | |
{ | |
documento.Print(); | |
} | |
} | |
private void documento_PrintPage(object sender, PrintPageEventArgs e) | |
{ | |
DocumentoTexto doc = (DocumentoTexto) sender; | |
using (Font fuente = new Font("Trebuchet", 10)) | |
{ | |
float altoLinea = fuente.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], fuente, Brushes.Black, x, y); | |
doc.Desplazamiento += 1; | |
y += altoLinea; | |
} | |
if (doc.Desplazamiento < doc.Texto.GetUpperBound(0)) | |
{ | |
e.HasMorePages = true; | |
} | |
else | |
{ | |
doc.Desplazamiento = 0; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment