PrintDocument: DocumentName, PrintPage, Print : Print PrintDocument « GUI Windows Forms « C# / CSharp Tutorial






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());
     }
}








23.51.Print PrintDocument
23.51.1.Basic Printing
23.51.2.Print BMP imagePrint BMP image
23.51.3.Multi Page Print
23.51.4.Subclass PrintDocument
23.51.5.Print a paragraph
23.51.6.The print preview application.
23.51.7.Print With Margins
23.51.8.Print PageSettings Metrics
23.51.9.PrintDocument: DocumentName, PrintPage, Print
23.51.10.The print process application.