Csharp/CSharp Tutorial/GUI Windows Forms/Print PrintDocument

Материал из .Net Framework эксперт
Версия от 12:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Basic Printing

using System;
using System.Drawing.Printing;
using System.Drawing;
  class PrintSample
  {
    [STAThread]
    static void Main(string[] args)
    {
      PrintSample oSample = new PrintSample();
      oSample.RunSample();
    }
    public void RunSample()
    {
        PrintDocument pd = new PrintDocument(); 
      pd.PrintPage += new PrintPageEventHandler(this.PrintPageEvent);
      pd.Print();
    }
    private void PrintPageEvent(object sender, PrintPageEventArgs ev) 
    {
      string strHello = "Hello Printer!";
      Font oFont = new Font("Arial",10);
      Rectangle marginRect = ev.MarginBounds;
      ev.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Black),marginRect);
      ev.Graphics.DrawString(strHello,oFont,new SolidBrush(System.Drawing.Color.Blue),
        (ev.PageBounds.Right/2), ev.PageBounds.Bottom/2);
    }
  }

Multi Page Print

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintDocumentSubClass : Form
{
    public PrintDocumentSubClass()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        this.cmdPrint.Location = new System.Drawing.Point(109, 122);
        this.cmdPrint.Size = new System.Drawing.Size(75, 23);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(278, 259);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Multi Page Print";
        this.ResumeLayout(false);
    }
    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDocument doc = new TextDocument();
        doc.PrintPage += this.Doc_PrintPage;
        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;
        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }
    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        TextDocument doc = (TextDocument)sender;
        Font font = new Font("Arial", 10);
        
        float lineHeight = font.GetHeight(e.Graphics);
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;
        doc.PageNumber += 1;
        while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset <= doc.Text.GetUpperBound(0))
        {
            e.Graphics.DrawString(doc.Text[doc.Offset], font,Brushes.Black, x, y);
            doc.Offset += 1;
            y += lineHeight;
        }
        if (doc.Offset < doc.Text.GetUpperBound(0))
        {
            e.HasMorePages = true;
        } else {
            doc.Offset = 0;
        }
        
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintDocumentSubClass());
    }
    private System.Windows.Forms.Button cmdPrint;
}
class TextDocument : PrintDocument{
    private string[] text;
    public string[] Text;
    public int PageNumber;
    public int Offset;
    
    public TextDocument()
    {
        this.Text = new string[100];
        for (int i = 0; i < 100; i++)
        {
            this.Text[i] += "string Text ";
        }
    }
}

Print a paragraph

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintParagraph : Form
{
    public PrintParagraph()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.cmdPrint.Location = new System.Drawing.Point(109, 122);
        this.cmdPrint.Size = new System.Drawing.Size(75, 23);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(282, 259);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Wrapped Print";
        this.ResumeLayout(false);
    }
    private void cmdPrint_Click(object sender, EventArgs e)
    {
        string text = "a paragraph";
        PrintDocument doc = new ParagraphDocument(text);
        doc.PrintPage += new PrintPageEventHandler(this.Doc_PrintPage);
        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;
        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }
    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        ParagraphDocument doc = (ParagraphDocument)sender;
        Font font = new Font("Arial", 15);
        e.Graphics.DrawString(doc.Text, font, Brushes.Black,
               e.MarginBounds, StringFormat.GenericDefault);
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintParagraph());
    }
    private System.Windows.Forms.Button cmdPrint;
    
}
public class ParagraphDocument : PrintDocument
{
    public string Text;
    public ParagraphDocument(string text)
    {
        this.Text = text;
    }
}

Print BMP image

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintDialogPrint : Form
{
    public PrintDialogPrint()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        this.cmdPrint.Location = new System.Drawing.Point(109, 122);
        this.cmdPrint.Size = new System.Drawing.Size(75, 23);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(272, 260);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Simple Print";
        this.ResumeLayout(false);
    }
    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDocument doc = new PrintDocument();
        doc.PrintPage += this.Doc_PrintPage;
        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;
        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }
    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Font font = new Font("Arial", 30);
        
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;
        float lineHeight = font.GetHeight(e.Graphics);
        for (int i = 0; i < 5; i++)
        {
            e.Graphics.DrawString("This is line " + i.ToString(),font, Brushes.Black, x, y);
            y += lineHeight;
        }
        y += lineHeight;
        e.Graphics.DrawImage(Image.FromFile("c:\\YourFile.bmp"), x, y);
        
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintDialogPrint());
    }
    private System.Windows.Forms.Button cmdPrint;
    
}

PrintDocument: DocumentName, PrintPage, Print

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
   
class HelloPrinter: Form
{
     public static void Main()
     {
          Application.Run(new HelloPrinter());
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics     grfx   = pea.Graphics;
          StringFormat strfmt = new StringFormat();
          grfx.DrawString("Click to print", Font, new SolidBrush(ForeColor),ClientRectangle, strfmt);
     }
     protected override void OnClick(EventArgs ea)
     {
          PrintDocument prndoc = new PrintDocument();
   
          prndoc.DocumentName = Text;
          prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
          prndoc.Print();
     }
     void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
     {
          Graphics grfx = ppea.Graphics;
          grfx.DrawString(Text, Font, Brushes.Black, 0, 0);
          SizeF sizef = grfx.MeasureString(Text, Font);
          grfx.DrawLine(Pens.Black, sizef.ToPointF(), grfx.VisibleClipBounds.Size.ToPointF());
     }
}

Print PageSettings Metrics

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

public class Form1 : Form {
    private Font mainTextFont = new Font("Times New Roman", 14);
    private Font subTextFont = new Font("Times New Roman", 12);
    private PageSettings storedPageSettings;
    public Form1() {
        InitializeComponent();
    }
    private void PaintDocument(Graphics g) {
        g.PageUnit = GraphicsUnit.Point;
        g.DrawString("Simple Printing Sample",
                     this.mainTextFont,
                     Brushes.Black,
                     new Rectangle(10, 20, 180, 30));
        g.DrawRectangle(Pens.Blue,
                        new Rectangle(new Point(10, 100), new Size(100, 50)));
    }
    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        PaintDocument(g);
    }
    private void menuFilePageSetup_Click(object sender, EventArgs e) {
        PageSetupDialog psDlg = new PageSetupDialog();
        if (this.storedPageSettings == null)
            this.storedPageSettings = new PageSettings();
        psDlg.PageSettings = this.storedPageSettings;
        psDlg.ShowDialog();
    }
    private void WriteMetricsToConsole(PrintPageEventArgs ev) {
        Graphics g = ev.Graphics;
        Console.WriteLine("ev.PageSettings.PaperSize: " + ev.PageSettings.PaperSize);
        Console.WriteLine("ev.PageSettings.PrinterResolution: " + ev.PageSettings.PrinterResolution);
        Console.WriteLine("ev.PageSettings.PrinterSettings.LandscapeAngle: " + ev.PageSettings.PrinterSettings.LandscapeAngle);
        Console.WriteLine("ev.PageSettings.Bounds: " + ev.PageSettings.Bounds);
        Console.WriteLine("ev.PageBounds: " + ev.PageBounds);
        Console.WriteLine("ev.PageSettings.Margins: " + ev.PageSettings.Margins);
        Console.WriteLine("ev.MarginBounds: " + ev.MarginBounds);
        Console.WriteLine("Horizontal resolution: " + g.DpiX);
        Console.WriteLine("Vertical resolution: " + g.DpiY);
        g.SetClip(ev.PageBounds);
        Console.WriteLine("g.VisibleClipBounds: " + g.VisibleClipBounds);
        SizeF drawingSurfaceSize = new SizeF( g.VisibleClipBounds.Width * g.DpiX / 100,g.VisibleClipBounds.Height * g.DpiY / 100);
        Console.WriteLine("Drawing Surface Size in Pixels: " + drawingSurfaceSize);
    }
    protected void PrintPageEventHandler(Object obj, PrintPageEventArgs ev) {
        WriteMetricsToConsole(ev);
        Graphics g = ev.Graphics;
        PaintDocument(g);
        ev.HasMorePages = false;
    }
    private void menuFilePrint_Click(object sender, EventArgs e) {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);
        if (this.storedPageSettings != null)
            pd.DefaultPageSettings = this.storedPageSettings;
        PrintDialog dlg = new PrintDialog();
        dlg.Document = pd;
        DialogResult result = dlg.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
            pd.Print();
    }
    private void menuFilePrintPreview_Click(object sender, EventArgs e) {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);
        if (this.storedPageSettings != null)
            pd.DefaultPageSettings = this.storedPageSettings;
        PrintPreviewDialog dlg = new PrintPreviewDialog();
        dlg.Document = pd;
        dlg.ShowDialog();
    }
    private void InitializeComponent() {
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePageSetup = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePrintPreview = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePrint = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Size = new System.Drawing.Size(292, 25);
        this.menuStrip1.Text = "menuStrip1";
        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuFilePageSetup,
            this.menuFilePrintPreview,
            this.menuFilePrint});
        this.fileToolStripMenuItem.Text = "File";
        this.menuFilePageSetup.Text = "Page Setup";
        this.menuFilePageSetup.Click += new System.EventHandler(this.menuFilePageSetup_Click);
        this.menuFilePrintPreview.Text = "Print Preview";
        this.menuFilePrintPreview.Click += new System.EventHandler(this.menuFilePrintPreview_Click);
        this.menuFilePrint.Text = "Print";
        this.menuFilePrint.Click += new System.EventHandler(this.menuFilePrint_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.Window;
        this.ClientSize = new System.Drawing.Size(292, 268);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Text = "SimplePrintingExample";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        this.menuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem menuFilePageSetup;
    private System.Windows.Forms.ToolStripMenuItem menuFilePrintPreview;
    private System.Windows.Forms.ToolStripMenuItem menuFilePrint;
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
}

Print With Margins

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class PrinterSelectionDialog: Form
{
     ComboBox combo;
   
     public PrinterSelectionDialog()
     {
          Label label    = new Label();
          label.Parent   = this;
          label.Text     = "Printer:";
          label.Location = new Point(8, 8);
          label.Size     = new Size(40, 8);
   
          combo = new ComboBox();
          combo.Parent   = this;
          combo.DropDownStyle = ComboBoxStyle.DropDownList;
          combo.Location = new Point(48, 8);
          combo.Size     = new Size(144, 8);
   
          foreach (string str in PrinterSettings.InstalledPrinters)
               combo.Items.Add(str);
   
          Button btn   = new Button();
          btn.Parent   = this;
          btn.Text     = "OK";
          btn.Location = new Point(40, 32);
          btn.Size     = new Size(40, 16);
          btn.DialogResult = DialogResult.OK;
          AcceptButton = btn;
   
          btn  = new Button();
          btn.Parent = this;
          btn.Text = "Cancel";
          btn.Location = new Point(120, 32);
          btn.Size   = new Size(40, 16);
          btn.DialogResult = DialogResult.Cancel;
   
          CancelButton = btn;
   
          ClientSize = new Size(200, 56);
          AutoScaleBaseSize = new Size(4, 8);
     }
     public string PrinterName
     {
          set { combo.SelectedItem = value; }
          get { return (string) combo.SelectedItem; }
     }
}
   
class PrintWithMargins: Form
{
     public static void Main()
     {
          Application.Run(new PrintWithMargins());
     }
     public PrintWithMargins()
     {
          Text = "Print with Margins";
          Menu = new MainMenu();
          Menu.MenuItems.Add("&File");
          Menu.MenuItems[0].MenuItems.Add("&Print...", 
                                   new EventHandler(MenuFilePrintOnClick));
     }
     void MenuFilePrintOnClick(object obj, EventArgs ea)
     {
          PrintDocument prndoc = new PrintDocument();
   
          PrinterSelectionDialog dlg = new PrinterSelectionDialog();
          dlg.PrinterName = prndoc.PrinterSettings.PrinterName;
   
          if (dlg.ShowDialog() != DialogResult.OK)
               return;
   
          prndoc.PrinterSettings.PrinterName = dlg.PrinterName;
   
          prndoc.DocumentName = Text;
          prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
          prndoc.Print();
     }
     void OnPrintPage(object obj, PrintPageEventArgs ppea)
     {
          Graphics   grfx  = ppea.Graphics;
          RectangleF rectf = new RectangleF(
               ppea.MarginBounds.Left - 
               (ppea.PageBounds.Width - grfx.VisibleClipBounds.Width) / 2,
               ppea.MarginBounds.Top - 
               (ppea.PageBounds.Height - grfx.VisibleClipBounds.Height) / 2,
               ppea.MarginBounds.Width,
               ppea.MarginBounds.Height);
   
          grfx.DrawRectangle(Pens.Black, rectf.X, rectf.Y, 
                                         rectf.Width, rectf.Height);
   
          grfx.DrawLine(Pens.Black, rectf.Left, rectf.Top, 
                                    rectf.Right, rectf.Bottom);
   
          grfx.DrawLine(Pens.Black, rectf.Right, rectf.Top, 
                                    rectf.Left, rectf.Bottom);
     }
}

Subclass PrintDocument

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintDocumentSubClass : Form
{
    public PrintDocumentSubClass()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        this.cmdPrint.Location = new System.Drawing.Point(109, 122);
        this.cmdPrint.Size = new System.Drawing.Size(75, 23);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(278, 259);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Multi Page Print";
        this.ResumeLayout(false);
    }
    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDocument doc = new TextDocument();
        doc.PrintPage += this.Doc_PrintPage;
        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;
        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }
    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        TextDocument doc = (TextDocument)sender;
        Font font = new Font("Arial", 10);
        
        float lineHeight = font.GetHeight(e.Graphics);
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;
        doc.PageNumber += 1;
        while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset <= doc.Text.GetUpperBound(0))
        {
            e.Graphics.DrawString(doc.Text[doc.Offset], font,Brushes.Black, x, y);
            doc.Offset += 1;
            y += lineHeight;
        }
        if (doc.Offset < doc.Text.GetUpperBound(0))
        {
            e.HasMorePages = true;
        } else {
            doc.Offset = 0;
        }
        
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintDocumentSubClass());
    }
    private System.Windows.Forms.Button cmdPrint;
}
class TextDocument : PrintDocument{
    private string[] text;
    public string[] Text;
    public int PageNumber;
    public int Offset;
    
    public TextDocument()
    {
        this.Text = new string[100];
        for (int i = 0; i < 100; i++)
        {
            this.Text[i] += "string Text ";
        }
    }
}

The print preview application.

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Button button1;
    private System.ruponentModel.Container components = null;
    private System.Drawing.Printing.PrintDocument ThePrintDocument = null;
    private System.IO.StringReader myStringReader = null;
    public Form1() {
        ThePrintDocument = new System.Drawing.Printing.PrintDocument();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.button1.Location = new System.Drawing.Point(112, 352);
        this.button1.Text = "&Preview";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(512, 397);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected void PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs ev) {
        float linesPerPage = 0;
        float yPosition = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;
        Font printFont = this.Font;
        SolidBrush myBrush = new SolidBrush(Color.Black);
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
        while (count < linesPerPage && ((line = myStringReader.ReadLine()) != null)) {
            yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
            count++;
        }
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
        myBrush.Dispose();
    }
    private void button1_Click(object sender, System.EventArgs e) {
        ThePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
        string strText = "STRING";
        myStringReader = new System.IO.StringReader(strText);
        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        printPreviewDialog1.Document = this.ThePrintDocument;
        printPreviewDialog1.ShowDialog();
    }
}

The print process application.

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.ruponentModel.Container components = null;
    private System.Windows.Forms.PrintDialog printDialog1;
    private System.Drawing.Printing.PrintDocument ThePrintDocument = null;
    private System.IO.StringReader myStringReader = null;
    public Form1() {
        ThePrintDocument = new System.Drawing.Printing.PrintDocument();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.printDialog1 = new System.Windows.Forms.PrintDialog();
        this.SuspendLayout();
        this.richTextBox1.Location = new System.Drawing.Point(72, 16);
        this.richTextBox1.Size = new System.Drawing.Size(344, 320);
        this.richTextBox1.Text = "richTextBox1";
        this.button1.Location = new System.Drawing.Point(128, 352);
        this.button1.Text = "&Print";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.button2.Location = new System.Drawing.Point(280, 352);
        this.button2.Text = "&Close";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(488, 381);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.button2,
        this.button1,
        this.richTextBox1});
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void button2_Click(object sender, System.EventArgs e) {
        Close();
    }
    private void button1_Click(object sender, System.EventArgs e) {
        ThePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
        printDialog1.Document = ThePrintDocument;
        string strText = this.richTextBox1.Text;
        myStringReader = new System.IO.StringReader(strText);
        if (printDialog1.ShowDialog() == DialogResult.OK) {
            this.ThePrintDocument.Print();
        }
    }
    protected void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev) {
        float linesPerPage = 0;
        float yPosition = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;
        Font printFont = this.richTextBox1.Font;
        SolidBrush myBrush = new SolidBrush(Color.Black);
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
        while (count < linesPerPage && ((line = myStringReader.ReadLine()) != null)) {
            yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, myBrush, leftMargin,yPosition, new StringFormat());
            count++;
        }
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
        myBrush.Dispose();
    }
}