30 octubre 2010

Impresion en .Net con C#

Aquí dejo el código en C# de un programa que muestra un formulario con un control TextBox y un Boton para mostrar en vista previa lo capturado en el TextBox.

Código del programa

//
// Programa que muestra como mostrar una vista previa e imprimir
// http://gsur.blogspot.com
//

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Impresion
{
class Formulario : Form
{
private System.ComponentModel.IContainer components = null;
private System.Drawing.Printing.PrintDocument ImprimeDocumento;
private System.Windows.Forms.PrintPreviewDialog DialogoVistaPrevia;
private System.Windows.Forms.Button btnImprime;
private System.Windows.Forms.TextBox txtNombre;
private System.Windows.Forms.Label lblNombre;
public Formulario()
{
ImprimeDocumento = new System.Drawing.Printing.PrintDocument();
DialogoVistaPrevia = new System.Windows.Forms.PrintPreviewDialog();
btnImprime = new System.Windows.Forms.Button();
txtNombre = new System.Windows.Forms.TextBox();
lblNombre = new System.Windows.Forms.Label();
ImprimeDocumento.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(ImprimeDocumento_ImprimePagina);
DialogoVistaPrevia.AutoScrollMargin = new System.Drawing.Size(0, 0);
DialogoVistaPrevia.AutoScrollMinSize = new System.Drawing.Size(0, 0);
DialogoVistaPrevia.ClientSize = new System.Drawing.Size(400, 300);
DialogoVistaPrevia.Document = ImprimeDocumento;
DialogoVistaPrevia.Enabled = true;
DialogoVistaPrevia.Name = "printPreviewDialog1";
DialogoVistaPrevia.Visible = false;
btnImprime.Location = new System.Drawing.Point(80, 64);
btnImprime.Name = "btnImprime";
btnImprime.Size = new System.Drawing.Size(80, 22);
btnImprime.TabIndex = 1;
btnImprime.Text = "Vista Previa";
btnImprime.UseVisualStyleBackColor = true;
btnImprime.Click += new System.EventHandler(btnImprime_Click);

txtNombre.Location = new System.Drawing.Point(80, 32);
txtNombre.Name = "txtNombre";
txtNombre.Size = new System.Drawing.Size(200, 22);
txtNombre.TabIndex = 0;
txtNombre.Text = "Escribir su nombre";
lblNombre.Location = new System.Drawing.Point(32, 32);
lblNombre.Name = "lblNombre";
lblNombre.Text = "Nombre: ";
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(322, 287);
Controls.Add(btnImprime);
Controls.Add(txtNombre);
Controls.Add(lblNombre);
Name = "frmImprime";
Text = "Impresion con C#";
}
private void ImprimeDocumento_ImprimePagina(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Nombre: " + txtNombre.Text, new Font("Arial", 16), System.Drawing.Brushes.Blue, new PointF(20,20));
Pen LineaNegra = new Pen(Color.FromArgb(255, 0, 0, 255), 5);
e.Graphics.DrawRectangle(LineaNegra, 120, 50, 450, 1);
}

private void btnImprime_Click(object sender, EventArgs e)
{
DialogoVistaPrevia.ShowDialog();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}

static class Programa
{
public static void Main()
{
Application.Run(new Formulario());
}
}
}

Imagen de la compilación del programa


Imagen del programa en ejecución

No hay comentarios.: